macromedia.coldfusion.cfml_general_discussion
[Top] [All Lists]

Re: Coldfusion Multiplication Error

Subject: Re: Coldfusion Multiplication Error
From: d1rty
Date: Tue, 8 Apr 2008 10:31:49 -0700 PDT
Newsgroups: macromedia.coldfusion.cfml_general_discussion

On Apr 8, 4:22 pm, "jdparlin" <webforumsu...@xxxxxxxxxxxxxx> wrote:
> I'm guessing you replied at the same time I was clarifying my need but as you 
> can see I am using percentages and I need both 3.2 and 3.7 to be 3
>
> Thanks

Ok, so there are essentially two things going on here..

1) you want to divide 58 by 100 and get a precise answer
2) you want to round all your results down, and fix will do this if 1)
is ok

so the answer to 1) is, you'll need to use java to do you calculations
directly, bypassing cf's flawed maths.

<cfset numShows = createObject("java",
"java.math.BigDecimal").init(javaCast("string", "58"))>
<cfset numApps = createObject("java",
"java.math.BigDecimal").init(javaCast("string", "100"))>
<cfset num100 = createObject("java",
"java.math.BigDecimal").init(javaCast("string", "100"))>
<cfset result = numShows.divide(numApps, javacast("int", 9), 1)>
<cfset percent = result.multiply(num100)>

<cfoutput>
result = #numberformat(result, "9.999999999999")# <br>
percent = #numberformat(percent, "9.999999999999")# <br>
fix = #fix(percent)# %
</cfoutput>

Chris

<Prev in Thread] Current Thread [Next in Thread>