[github-321] Fix issue with rounding in DataFormatter. First try broke a test.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899686 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-04-09 13:55:25 +00:00
parent 4682903e6a
commit be2a929f44
1 changed files with 8 additions and 1 deletions

View File

@ -950,7 +950,14 @@ public class DataFormatter {
if (numberFormat == null) {
return Double.toString(d);
}
String formatted = numberFormat.format(new BigDecimal(Double.toString(d)));
String formatted;
try {
//see https://github.com/apache/poi/pull/321 -- but this sometimes fails as Double.toString
//can produce strings that can't be parsed by BigDecimal
formatted = numberFormat.format(new BigDecimal(Double.toString(d)));
} catch (NumberFormatException nfe) {
formatted = numberFormat.format(d);
}
return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
}