Looks like it does have something to do with the max value / floating point rounding errors.
BigDecimal db = new BigDecimal(10099999.0499f);
if you change the "f" to a "d" - pass it in as a double you get:
db: 10099999.04990000091493129730224609375
Whole: 10099999
Remainder: 0.04990000091493129730224609375
Compare: 1
If you change it to a String - let big decimal handle it you get the behaviour you expect - not rounding or whatever errors.
db: 10099999.0499
Whole: 10099999
Remainder: 0.0499
Compare: 1
Pass the number into the bigdecimal as a string - if you don't you're constraining the calculation by the floating point issues.
Michael Wiles
Java Developer
| "Carl Woermann"
<[EMAIL PROTECTED]>
Sent by: [email protected] 2006/10/19 09:34 AM
|
|
Hi all
Ive been getting funny bugs with formatting floats, having to round them according to unconventional rules etc, so after some googling I decided to use Big Decimal, which apparentlay makes it all good. To my surprise I started getting the same errors. So I've narrowed the source of all problems down to this a strange oddity with is best illustrated in the code below:
BigDecimal db = new BigDecimal(10099999.0499f);
System.out.println("db: " + db); // prints: db: 10099999
BigDecimal whole = db.setScale (0, BigDecimal.ROUND_FLOOR); // does not modify original value according to javadoc
System.out.println("Whole: " + whole); // prints: Whole: 10099999
BigDecimal remainder = db.subtract (whole); // should leave me with 0.049...
System.out.println("Remainder: " + remainder ); // prints: Remainder: 0
// See if it is really Zero
System.out.println("Compare: " +remainder.compareTo(new BigDecimal(0))); // prints Compare: 0
// So where is my fraction of 0.0.49 gone?
Note that the above code behave as expected if you lower the number eg 9999.049f - I haven't bothered to try to find the exact number at which it breaks, and a check if the float is bigger that Float.MaxValue also passes.
Anybody wrote any accounting or banking app lately? ( My mind wanders to apps deployed in Zim for example.. )
Thanks - Carl
Disclaimer
Sanlam Life Insurance Limited Reg no 1998/021121/06 - Licensed Financial Services Provider
Disclaimer and Directors
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---
