Solved

ColdFusion Can't Subtract Decimals

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
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…
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 exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

707 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

9 Experts available now in Live!

Get 1:1 Help Now