tracker issue : CF-3822899

select a category, or use search below
(searches all categories and all time range)
Title:

Round produces wrong number when

| View in Tracker

Status/Resolution/Reason: Closed/Withdrawn/NotABug

Reporter/Name(from Bugbase): M B / M B (bean5c)

Created: 09/11/2014

Components: Language

Versions: 10.0

Failure Type: Incorrect w/Workaround

Found In Build/Fixed In Build: Final /

Priority/Frequency: Major / All users will encounter

Locale/System: English / Win All

Vote Count: 0

Problem Description:
Round() sometimes return incorrect result. We have only proven that this is the case when an expression is passed in that that ends in .5 (but not all of them do this). It might happen at other times as well.

Steps to Reproduce:
Call: Round(575/1000*100)
Call: Round(23/40*100)

Actual Result:
57

Expected Result:
58

Any Workarounds:
Two workarounds exist. The first is to cast the result of the expression as a float:
Round(javaCast("float", 23/40*100")

The second workaround is to pass it as a string:
Round("" & 23/40*100")

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3822899

External Customer Info:
External Company:  
External Customer Name: bean5c
External Customer Email:  
External Test Config: My Hardware and Environment details:

Attachments:

Comments:

Strangely, when calling Round(575/1000*100), the result is incorrect while calling Round(505/1000*100) the result is correct. Also, we don't like having to call java code in CF.
Comment by External U.
10920 | September 11, 2014 01:27:13 PM GMT
Further testing reveals that calling javaCast is sometimes detrimental while the string version is sometimes detrimental. For example, Round("" & 0.694999999999 * 100) is not equal to the javaCast version, but this one is more correct. For example, Round("" & 0.694999999999 * 100) is not equal to Round(0.694999999999 * 100), and is not correct.
Comment by External U.
10921 | September 11, 2014 01:41:55 PM GMT
Sorry, I may have fed misinformation with a previous comment. It appears that the best work-around is to cast to string before calling as demonstrated in the below extract (taken from a .cfm): <cfset i = 23/40*100 /> Number in question (based on an expression): #i#<br /> Normal Round: #Round(i)#<br /> javaCast: #Round(javaCast('float', i))#<br /> String: #Round("" & i)#<br /> Expected: 58<br /><br /> <cfset i = 0.005 /> Number in question: #i#<br /> Normal Round: #Round(i*100)#<br /> javaCast: #Round(javaCast('float', i*100))#<br /> String: #Round("" & i*100)#<br /> Expected: 1<br /><br /> <br /><br /> <cfset i = 0.694999999999 /> Number in question: #i#<br /> Normal Round: #Round(i*100)#<br /> javaCast: #Round(javaCast('float', i*100))#<br /> String: #Round("" & i*100)#<br /> Expected: 69<br /><br /> <br /><br /> Conversion to string works in every case while javaCast will produce incorrect results as compared to not using any work-around at all.
Comment by External U.
10922 | September 11, 2014 03:42:46 PM GMT
This behavior has nothing to do with ColdFusion. This is because of the way floating point numbers are represented. If you need precision, you should use PrecisionEvaluate. You would need to change these two expressions as round(precisionEvaluate(575/1000*100)) and round(precisionEvaluate(23/40*100))
Comment by Rupesh K.
10923 | September 11, 2014 11:30:50 PM GMT
Thanks for the input! We tested and sure enough, calling precisionEvaluate() does return values such that round() will correctly compute.
Comment by External U.
10924 | September 29, 2014 11:35:05 AM GMT