Bug 56741

Summary: [PATCH] Floating point rounding problems with ROUNDDOWN function
Product: POI Reporter: Cédric Walter <cedric.walter>
Component: SS CommonAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal Keywords: PatchAvailable
Priority: P2    
Version: 3.11-dev   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Attachments: Patch.txt
test-data/spreadsheet/FormulaEvalTestData.xls

Description Cédric Walter 2014-07-18 14:32:40 UTC
Similar to the patch provided in Bug 56315
the ROUNDDOWN() function is not behaving like excel and required a similar patch as for the ROUND() function.

New code

public static double roundDown(double n, int p) {
 double retval;
 if (Double.isNaN(n) || Double.isInfinite(n)) {
   retval = Double.NaN;
 } else {
   retval = new BigDecimal(NumberToTextConverter.toText(n)).setScale(p, java.math.RoundingMode.DOWN).doubleValue();
 }
 return retval;
}

I will provide a patch, unit tests and integration test FormulaEvalTestData.xls soon
Comment 1 Cédric Walter 2014-07-18 14:57:29 UTC
Created attachment 31829 [details]
Patch.txt
Comment 2 Cédric Walter 2014-07-18 14:58:01 UTC
Created attachment 31830 [details]
test-data/spreadsheet/FormulaEvalTestData.xls

Integration test
Comment 3 Cédric Walter 2014-07-23 10:07:11 UTC
this patch break the ROUNDDOWN method in "some" cases and should not be merged!

Patch require further analysis

BTW the rounddup function also do not behave in POI like in excel and will hopefully be solved soon.
Comment 4 Dominik Stadler 2019-03-10 16:40:03 UTC
There were some changes to the methods in the meantime, so I think this is mostly fixed now, if not, then please reopen this bug with updated information.

The latest fix was applied via r1801798: 
github #43: fix roundUp and roundDown. Thanks to @FishMeat on github for the patch.