Copy link to clipboard
Copied
This is an issue I have on CF2016, but I've tried CF Fiddle and it's the same result on all versions, so I presume it's not a bug.
I noticed that calculations were not rounding correctly, for example if the calculation gave a result of 7.875 and I used NumberFormat to round it to two decimal places, I would have expected it to round up to 7.88
After a lot of playing around, and a few conversations with ChatGTP 🙂 I hacked around and found something unusual, if I change the value to a string, within the NumberFormat, it works.
Here is the code for the two tests with and without the ToString. Notice the different in rounding. Am I missing something? It gives me the right answer and I can't seem to break it, but it appears to be an odd way to go about things
<CFOUTPUT>
<b>Test 1</b>
<br>
Before rounding: #aff_payment#
<br>
After NumberFormat rounding: <b>#NumberFormat(aff_payment,"9999999.99")#</b>
<br><br>
<b>Test 2</b>
<br>
Before rounding: #aff_payment#
<br>
After NumberFormat rounding with ToString: <b>#NumberFormat(ToString(aff_payment),"9999999.99")#</b>
</CFOUTPUT>
Result
Test 1
Before rounding: 7.875
After NumberFormat rounding: 7.87
Test 2
Before rounding: 7.875
After NumberFormat rounding with ToString: 7.88
2 Correct answers
I ran the code, as below, on https://trycf.com
<cfset aff_payment=7.875>
<cfoutput>
<b>Test 1</b>
<br>
Before rounding: #aff_payment#
<br>
After NumberFormat rounding: <b>#NumberFormat(aff_payment,"9999999.99")#</b>
<br><br>
<b>Test 2</b>
<br>
Before rounding: #aff_payment#
<br>
After NumberFormat rounding with ToString: <b>#NumberFormat(ToString(aff_payment),"9999999.99")#</b>
</cfoutput>
I used each of the following 10 CFML engines in turn:
However, none of them reproduces the 7.
...I have to admit I can not remember what the outcome was at my side as it was almost 6 monhs ago. I searched threough the code I was working on and don't see me using the string approach. I also re-tried the code above (on CF2016) and it gave the right result. I have no idea what happened.
Copy link to clipboard
Copied
I ran the code, as below, on https://trycf.com
<cfset aff_payment=7.875>
<cfoutput>
<b>Test 1</b>
<br>
Before rounding: #aff_payment#
<br>
After NumberFormat rounding: <b>#NumberFormat(aff_payment,"9999999.99")#</b>
<br><br>
<b>Test 2</b>
<br>
Before rounding: #aff_payment#
<br>
After NumberFormat rounding with ToString: <b>#NumberFormat(ToString(aff_payment),"9999999.99")#</b>
</cfoutput>
I used each of the following 10 CFML engines in turn:
However, none of them reproduces the 7.87 result. In each case, the result is 7.88 for Test 1 and for Test 2.
Copy link to clipboard
Copied
I have to admit I can not remember what the outcome was at my side as it was almost 6 monhs ago. I searched threough the code I was working on and don't see me using the string approach. I also re-tried the code above (on CF2016) and it gave the right result. I have no idea what happened.

