Skip to main content
Participating Frequently
April 7, 2008
Answered

Coldfusion Multiplication Error

  • April 7, 2008
  • 1 reply
  • 3322 views
I am running Server Version: 6,1,0,63958 and .58*100 is evaluation to 57.

Is there a patch to fix this and are there any other known equations that evaluate wrong? I might have to correct a lot of historical statistics.
    This topic has been closed for replies.
    Correct answer Mr_Black
    Adam,

    That is extremely close Thanks!

    I have been looking for a java method similar to Int() so that I can truncate the decimals on the non whole percentages. I tried Int again but it still causes the problem.

    I'm also trying to use Math.Floor() or intValue() but I don't know the correct java syntax.

    Thanks Again

    As I said already there is nothing wrong with CF or Java. This is perfectly normal. Your solution is (assuming that you are working with percentages):

    <!--- Calculate --->
    <cfset percent=number*100>
    <!--- Round to one decimal after floating point --->
    <cfset percent=NumberFormat(percent, "99.9")>
    <!--- Truncate --->
    <cfset percent=Int(percent)>

    Or all together:

    <cfset percent=Int(NumberFormat(number*100, "99.9"))>


    1 reply

    Inspiring
    April 7, 2008
    > .58*100 is evaluation to 57.

    How is the value of 0.58 generated?

    Are you aware of the inherent inaccuracies in floating-point arithmetic?

    --
    Adam
    jdparlinAuthor
    Participating Frequently
    April 7, 2008
    It sounds like it might be a float problem. Here are my test cases

    Evaluates to 58
    <cfoutput>#Int(58)#</cfoutput>

    Evaluates to 58
    <cfoutput>#(58/100)*100#</cfoutput>

    Evaluates to 59
    <cfoutput>#Int((59/100)*100)#</cfoutput>

    Problem - Evaluates to 57
    <cfoutput>#Int((58/100)*100)#</cfoutput>

    I was thinking of checking isValid("integer", X) before applying the Int() function but isValid() doesn't exist in 6.1. Any other fix ideas?

    Thanks

    Thanks
    Participating Frequently
    April 7, 2008
    This was discussed many times...

    There is no error in CF. There is error in your code. You expect your computer to do decimal math, while computer performs binary math. Therefore, floating-point numbers that look integers almost never are integers, unless you force them to be so.

    Try:

    <cfoutput>#NumberFormat((58/100)*100, "99.9999999999999999")#</cfoutput>

    and you will see that the result is slightly off the desired value of 58. When you use Int(), the CF cuts off the entire decimal part and you get 57. So use Round(), or NumberFormat() to display the result. Of course, it has nothing to do with the "evaluation".