Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

CF rounding issues

Enthusiast ,
Nov 16, 2023 Nov 16, 2023

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

3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Apr 06, 2024 Apr 06, 2024

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:

BKBK_0-1712415420194.pngexpand image

 

However, none of them reproduces the 7.

...
Translate
Enthusiast , Apr 10, 2024 Apr 10, 2024

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.

Translate
Community Expert ,
Apr 06, 2024 Apr 06, 2024

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:

BKBK_0-1712415420194.pngexpand image

 

However, none of them reproduces the 7.87 result. In each case, the result is 7.88 for Test 1 and for Test 2.

 

BKBK_1-1712416035290.pngexpand image

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 10, 2024 Apr 10, 2024
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources