Solved

ColdFusion Can't Subtract Decimals

Posted on 2006-06-15
2
657 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
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now