Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is up with this in ColdFusion's DecimalFormat() function? How do I get the correct result?

Tags:

<cfset number1 = 20.5/80 * 100 />
<cfset number2 = 18.125 />
<cfset number3 = 6.875 />

<cfoutput>
DecimalFormat(#number1#): #DecimalFormat(number1)#<br />
DecimalFormat(#number2#): #DecimalFormat(number2)#<br />
DecimalFormat(#number3#): #DecimalFormat(number3)#
</cfoutput>

OUTPUTS:

DecimalFormat(25.625): 25.62

DecimalFormat(18.125): 18.13

DecimalFormat(6.875): 6.88

RATHER THAN OUTPUTING:

DecimalFormat(25.625): 25.63

DecimalFormat(18.125): 18.13

DecimalFormat(6.875): 6.88

It seems that a variable that is the result of a mathematical calculation makes DecimalFormat() behave differently. Any quick fix, without digging into java?

like image 784
Jayson Avatar asked Sep 15 '09 16:09

Jayson


1 Answers

I think the problem is not DecimalFormat(), but the typical floating-point rounding errors.

see: PrecisionEvaluate()

like image 80
Henry Avatar answered Oct 11 '22 18:10

Henry