I’ve got a weird rounding issue with Lucee 5.
To reproduce, paste this Code on trycf.com:
<cfscript> price=4.1; discount=25; result1 = price - (price*discount/100); // 4.1 - 25% => 3.075 writeOutput('result1 => ' & result1 & '<hr />'); // outputs 3.075 resultRounded = Round(result1,2); // this shoud round to 3.08 //resultRounded = NumberFormat(result1,'99999.99'); // workaound, but not nice!? writeOutput('resultRounded (calculted) => ' & resultRounded & '<hr />'); // outputs 3.07 => wrong! result2=3.075; resultRounded2 = Round(result2,2); // this should round to 3.08 writeOutput('resultRounded2 (fix) => ' & resultRounded2 & '<hr />'); // outputs 3.08 => correct! dump(var=result1,label='result1'); dump(var=result2,label='result2'); if (result1 == result2) writeOutput('results are the same'); else writeoutput(result1 & ' and ' & result2 & ' are not equal!'); </cfscript>
Explanation: a price is calculated; it should be 25% of 4.1 which will be 3.075.
The first output just shows the calculated price; 3.075 => correct.
Now, the result should be rounded mathematically to two decimals; result should be 3.08.
BUT => resultRounded outputs 3.07 => wrong!
If I directly set result=3.075 and then do the rounding to that result; output is 3.08 => correct!
Also take a look at the last 3 lines => a dump of both - result1 and result2 - shows both are numbers and both with 3.075. But a comparison of those outputs that they are NOT equal!
Is this a bug?
In the code you see a workaround; if I would use “NumberFormat” of result1 than it will output 3.08. But that’s not “nice”; and also resultRounded would be a string not a number (of course, I could change it to a number with Val(), but that’s definitely not what I want…)