colinwjd commented on code in PR #321:
URL: https://github.com/apache/poi/pull/321#discussion_r845701701
##########
poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java:
##########
@@ -950,7 +950,7 @@ private String getFormattedNumberString(Cell cell,
ConditionalFormattingEvaluato
if (numberFormat == null) {
return String.valueOf(d);
}
- String formatted = numberFormat.format(d);
+ String formatted = numberFormat.format(new
BigDecimal(String.valueOf(d)));
Review Comment:
<img width="190" alt="image"
src="https://user-images.githubusercontent.com/7942918/162353067-28a23e52-9ff7-425b-93c2-891e2330ed69.png">
<img width="850" alt="image"
src="https://user-images.githubusercontent.com/7942918/162353213-43151a9e-d10f-425d-a50b-2e46ed792071.png">
As shown above, the value 2.05 is correctly rounded to 2.1 in excel, but in
the parsed result the value is 2.0
The reason is that the Double type has a loss of precision, so we need to
use the BigDecimal type.
We need to use the String type to initialize the BigDecimal object,
otherwise there will also be precision problems.
It is described in the Java api documentation as follows
> This is generally the preferred way to convert a float or double into a
BigDecimal, as it doesn't suffer from the unpredictability of the
BigDecimal(double) constructor.
<img width="678" alt="image"
src="https://user-images.githubusercontent.com/7942918/162354520-27fbc225-62c9-478b-a065-5cbbc97ad5d4.png">
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]