diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java index 101fa6c10e..18254af451 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java @@ -29,6 +29,7 @@ import java.util.regex.Pattern; import javax.swing.JLabel; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.format.CellFormat; import org.apache.poi.ss.format.CellFormatPart; @@ -217,4 +218,22 @@ class TestCellFormatPart { } } } + + @Test + void testDecimalFormat() throws Exception { + // Create a workbook, row and cell to test with + try (Workbook wb = new HSSFWorkbook()) { + Sheet sheet = wb.createSheet(); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + + CellFormat cf = CellFormat.getInstance("[<=.01]0.00%;#,##0"); + + cell.setCellValue(1); + assertEquals("1", cf.apply(cell).text); + + cell.setCellValue(.001); + assertEquals("0.10%", cf.apply(cell).text); + } + } } diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java b/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java index c505314ae0..b86e7aba55 100644 --- a/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java +++ b/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java @@ -104,7 +104,7 @@ public class CellFormatPart { static { // A condition specification String condition = "([<>=]=?|!=|<>) # The operator\n" + - " \\s*(-?[0-9]+(?:\\.[0-9]*)?)\\s* # The constant to test against\n"; + " \\s*(-?([0-9]+(?:\\.[0-9]*)?)|(\\.[0-9]*))\\s* # The constant to test against\n";; // A currency symbol / string, in a specific locale String currency = "(\\[\\$.{0,3}(-[0-9a-f]{3,4})?\\])";