return sb.toString();
}
+ private static class InternalDecimalFormatWithScale extends Format {
+
+ private static final Pattern endsWithCommas = Pattern.compile("(,+)$");
+ private BigDecimal divider;
+ private static final BigDecimal ONE_THOUSAND = new BigDecimal(1000);
+ private final DecimalFormat df;
+ private static final String trimTrailingCommas(String s) {
+ return s.replaceAll(",+$", "");
+ }
+
+ public InternalDecimalFormatWithScale(String pattern, DecimalFormatSymbols symbols) {
+ df = new DecimalFormat(trimTrailingCommas(pattern), symbols);
+ setExcelStyleRoundingMode(df);
+ Matcher endsWithCommasMatcher = endsWithCommas.matcher(pattern);
+ if (endsWithCommasMatcher.find()) {
+ String commas = (endsWithCommasMatcher.group(1));
+ BigDecimal temp = BigDecimal.ONE;
+ for (int i = 0; i < commas.length(); ++i) {
+ temp = temp.multiply(ONE_THOUSAND);
+ }
+ divider = temp;
+ } else {
+ divider = null;
+ }
+ }
+
+ private Object scaleInput(Object obj) {
+ if (divider != null) {
+ if (obj instanceof BigDecimal) {
+ obj = ((BigDecimal) obj).divide(divider, RoundingMode.HALF_UP);
+ } else if (obj instanceof Double) {
+ obj = (Double) obj / divider.doubleValue();
+ } else {
+ throw new UnsupportedOperationException();
+ }
+ }
+ return obj;
+ }
+
+ @Override
+ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
+ obj = scaleInput(obj);
+ return df.format(obj, toAppendTo, pos);
+ }
+
+ @Override
+ public Object parseObject(String source, ParsePosition pos) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
private Format createNumberFormat(String formatStr, double cellValue) {
String format = cleanFormatForNumber(formatStr);
DecimalFormatSymbols symbols = decimalSymbols;
}
try {
- DecimalFormat df = new DecimalFormat(format, symbols);
- setExcelStyleRoundingMode(df);
- return df;
+ return new InternalDecimalFormatWithScale(format, symbols);
} catch(IllegalArgumentException iae) {
logger.log(POILogger.DEBUG, "Formatting failed for format " + formatStr + ", falling back", iae);
// the pattern could not be parsed correctly,
return null; // Not supported
}
}
-}
+}
\ No newline at end of file
);
}
}
-
+
@Test
public void testConditionalRanges() {
DataFormatter dfUS = new DataFormatter(Locale.US);
wb.close();
}
-
+
+ @Test
+ public void testFormatWithTrailingDotsUS() {
+ DataFormatter dfUS = new DataFormatter(Locale.US);
+ assertEquals("1,000,000", dfUS.formatRawCellContents(1000000, -1, "#,##0"));
+ assertEquals("1,000", dfUS.formatRawCellContents(1000000, -1, "#,##0,"));
+ assertEquals("1", dfUS.formatRawCellContents(1000000, -1, "#,##0,,"));
+ assertEquals("1,000,000.0", dfUS.formatRawCellContents(1000000, -1, "#,##0.0"));
+ assertEquals("1,000.0", dfUS.formatRawCellContents(1000000, -1, "#,##0.0,"));
+ assertEquals("1.0", dfUS.formatRawCellContents(1000000, -1, "#,##0.0,,"));
+ assertEquals("1,000,000.00", dfUS.formatRawCellContents(1000000, -1, "#,##0.00"));
+ assertEquals("1,000.00", dfUS.formatRawCellContents(1000000, -1, "#,##0.00,"));
+ assertEquals("1.00", dfUS.formatRawCellContents(1000000, -1, "#,##0.00,,"));
+ assertEquals("1,000,000", dfUS.formatRawCellContents(1e24, -1, "#,##0,,,,,,"));
+ }
+
+ @Test
+ public void testFormatWithTrailingDotsOtherLocale() throws Exception {
+ DataFormatter dfIT = new DataFormatter(Locale.ITALY);
+ assertEquals("1.000.000", dfIT.formatRawCellContents(1000000, -1, "#,##0"));
+ assertEquals("1.000", dfIT.formatRawCellContents(1000000, -1, "#,##0,"));
+ assertEquals("1", dfIT.formatRawCellContents(1000000, -1, "#,##0,,"));
+ assertEquals("1.000.000,0", dfIT.formatRawCellContents(1000000, -1, "#,##0.0"));
+ assertEquals("1.000,0", dfIT.formatRawCellContents(1000000, -1, "#,##0.0,"));
+ assertEquals("1,0", dfIT.formatRawCellContents(1000000, -1, "#,##0.0,,"));
+ assertEquals("1.000.000,00", dfIT.formatRawCellContents(1000000, -1, "#,##0.00"));
+ assertEquals("1.000,00", dfIT.formatRawCellContents(1000000, -1, "#,##0.00,"));
+ assertEquals("1,00", dfIT.formatRawCellContents(1000000, -1, "#,##0.00,,"));
+ assertEquals("1.000.000", dfIT.formatRawCellContents(1e24, -1, "#,##0,,,,,,"));
+ }
+
/**
* bug 60031: DataFormatter parses months incorrectly when put at the end of date segment
*/
assertEquals("4,33 " + euro, df.formatRawCellContents(4.33, 178, formatString));
assertEquals("1.234,33 " + euro, df.formatRawCellContents(1234.33, 178, formatString));
}
-}
+}
\ No newline at end of file