Solved

ColdFusion Can't Subtract Decimals

Posted on 2006-06-15
2
705 Views
Last Modified: 2013-12-24
OK, This is a nutty one.

Run this code and tell me what result you get.
      <cfset number_one = 179.03>
      <cfset number_two = 179.02>
      <cfset the_difference = number_one - number_two>
      <cfoutput>
      #the_difference#
      </cfoutput>

I should mention I am running CFMX 7.01 on Win2K3

The result I get is 0.00999999999999 and I have checked it repeatedly and run JUST that function on the page with no other code at all... same result.

Now I know I can round but the mere fact that ColdFusion can't subtract decimal figures is particularly frightening. Its is simply not feasible to place the round() function on every use of a fraction in an application.

Anyone have any ideas what is causing this?

** More info - only seems to happen when result is the result is (should be) 0.01 as I tested lots of other options which worked fine. This is more bizarre by the minute.
0
Comment
Question by:cf_doggie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
meverest earned 350 total points
ID: 16911563
This effect is an unfortunate fact of life with floating point data types.  it is less a bug in coldfusion than a limitation of floating point formatting.  The problem here is that coldfusion autoatically converts the numric to a floating point value, does the calculation, and then displays the float result in text form.

If the data you are working with is only ever two decimal places (for example currency), then the common tactic is to work only with integers (eg 'cents') and then do a divide by 100 conversion at display time.

Here is an intersting treatment of some floating point issues:

http://blogs.msdn.com/ericlippert/archive/2005/01/13/352284.aspx
0
 
LVL 7

Assisted Solution

by:ExpertAdmin
ExpertAdmin earned 150 total points
ID: 16915388
I have solved this problem by falling back on the Java that underpins ColdFusion. Since CF is an extension of Java, you can use java functions within it. There is a Java function called Javacast that can convert numbers to various levels of precision. For example:

<cfoutput>#javacast("double", (number_1 - number_2))#</cfoutput>

This should return the correct decimal value.

Alternately you could do this:

<cfoutput>#javacast("double", number_1) - javacast("double" number_2)#</cfoutput>

Hope this helps.

M@
0

Featured Post

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What You Need to Know when Searching for a Webhost Provider
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question