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
Solved

ColdFusion Can't Subtract Decimals

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
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 create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

829 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