Isn’t 5 the number at which a rounding is supposed to go up?
Look at the code below;
var amount as double = 10.125
var amountString as string = format (amount, “####.00”)
This results in amountString being “10.12” instead of “10.13”
Isn’t 5 the number at which a rounding is supposed to go up?
Look at the code below;
var amount as double = 10.125
var amountString as string = format (amount, “####.00”)
This results in amountString being “10.12” instead of “10.13”
This has been noted before on the forum.
https://forum.xojo.com/t/rounding-numbers/17100
MessageBox(Format(2.15, “0.0”)) → 2.1
MessageBox(Format(2.25, “0.0”)) → 2.2
MessageBox(Format(2.35, “0.0”)) → 2.4
MessageBox(Format(2.45, “0.0”)) → 2.5
MessageBox(Format(2.55, “0.0”)) → 2.5
MessageBox(Format(2.65, “0.0”)) → 2.6
The ensuing discussion attributed this behavior to the fact that things like 2.25 are actually Doubles and not exact.
“2.25 is stored as a Double, and is actually 2.24999(repeat), so it rounds down”
The discussion includes various strategies to get around this problem if it is important.
Thanks, I’ll use the + 0.005 before format as a fix.
Alternatively, @John_Fatte has a really great solution within Rounding dilemma. With this solution you can round to any number of decimal places. This thread also has various other solutions as well.
Also not sure about your use case, but based upon the format string you have listed, if this happens to be a monetary value, then the currency data type is a great way to avoid much of the issues with doubles.
Through the years the currency data type has had various bugs with it and I believe the major ones have been resolved but there are still various mostly edge cases still open. @Rick_Araujo has created a separate Money Class as a replacement. Here’s the background and the I believe the latest version is here.
Also depending upon your use case, you may want to address - 0.005 as well if you anticipate the value you’re rounding to come in negative.
Can you tell I’ve recently been down these rabbit trails?