Fix the rounding issue for numeric type formatting

Resolve the current issue with the DecimalFormat class when the incoming obj is of type Double or Float. For example, if a Double type value of 1.005 is passed in, and the corresponding cell in Excel is of numeric type and set to keep two decimal places.  Formatting the Double value 1.005 will be problematic; it will be formatted as 1.00, which is inconsistent with the behavior of Excel and does not perform rounding correctly
This commit is contained in:
YUFEIFUT 2024-07-01 13:44:24 +08:00 committed by GitHub
parent 527f26aac9
commit 7f56717398
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 0 deletions

View File

@ -823,6 +823,14 @@ public class DataFormatter {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
obj = scaleInput(obj);
// Resolve the current issue with the DecimalFormat class when the incoming obj is of type Double or Float
// For example, if a Double type value of 1.005 is passed in, and the corresponding cell in Excel is of numeric type
// and set to keep two decimal places
// Formatting the Double value 1.005 will be problematic; it will be formatted as 1.00, which is inconsistent with
// the behavior of Excel and does not perform rounding correctly
if (obj instanceof Double || obj instanceof Float) {
return df.format(new BigDecimal(obj.toString()), toAppendTo, pos);
}
return df.format(obj, toAppendTo, pos);
}