?
Solved

ColdFusion Can't Subtract Decimals

Posted on 2006-06-15
2
Medium Priority
?
723 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 1400 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 600 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
Suggested Courses

807 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