views:

532

answers:

3
<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?

+1  A: 

DecimalFormat is a formatting function. Not a Mathematical function. Its job is not to round your number for you, unfortunately CF lacks good mathematical functions for decimals so you will have to write your own.

Here is one someone wrote on the CF livedocs page for round():

 <cffunction name="roundDecimal" returntype="numeric"> 
     <cfargument name="num" type="numeric" required="yes"> 
     <cfargument name="decimal" type="numeric" default="2" required="no"> 

     <cfreturn round(num*10^decimal)/10^decimal /> 
  </cffunction>
ryber
True, it is a formatting function, and I was using it for output, but needed it to function consistently across values whether calculated or not. But, good solution if you need to round numbers for other means...
Jayson
+2  A: 

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

see: PrecisionEvaluate()

Henry
Shouldn't be any floating-point errors, all of these numbers can be expressed exactly as a base-2 floating-point number. (0.625 = 5/8, 0.125 = 1/8, 0.875 = 7/8. Since 8 is a power of 2, there would be no roundoff error.) I'm assuming a `double` or `float` is used under the covers...
Kip
that said, i can't think of any reason for the results he's seeing
Kip
number1 = PrecisionEvaluate(20.5/80 * 100) will get you 25.62500000000000000000 -> 25.63
Henry
using PrecisionEvaluate() in calculations makes the DecimalFormat() function behave more consistantly! :)
Jayson
A: 

Please see the CF documentation NumberFormat and do a page search on round to see some specific information.

Jay