Hi guys! I just found and verified a very hard-to-notice bug with the NumberFormat() function when used on BigDecimal values with a lot of digits after the decimal (12 or more).
Lucee’s implementation of BigDecimal seems to allow up to 16 digits after the decimal point. If you use NumberFormat(), it’ll cause the 12th position to be one digit higher than it should be, and with certain values like 9 the error will propagate in spectacular fashion to places 1 thru 11, too. Run this in Lucee 6 (with “Precise Math” turned on in the admin) to see what I mean:
<cfset my_number = 0.1111111111111111>
<cfset my_formatted_number = NumberFormat(my_number,"0.0000000000000000")>
<cfdump eval="my_number">
<cfdump eval="my_formatted_number"><hr>
<cfset my_number = 0.9999999999999999>
<cfset my_formatted_number = NumberFormat(my_number,"0.0000000000000000")>
<cfdump eval="my_number">
<cfdump eval="my_formatted_number"><hr>
<cfset my_number = 0.4156616876960124>
<cfset my_formatted_number = NumberFormat(my_number,"0.0000000000000000")>
<cfdump eval="my_number">
<cfdump eval="my_formatted_number">
The formatted numbers output are these (run it yourself to verify):
0.1111111111121111 (2 not 1 in 12th position)
1.0000000000009999 (wow!)
0.4156616876970124 (7 not 6 in 12th position)
When not using BigDecimal and using double instead (i.e. in Lucee versions before 6, or in version 6 when “Precise Math” is turned off in the admin), Lucee supports up to 12 digits after the decimal point. Because this bug causes inaccuracy in the 12th digit, I suspect the NumberFormat() function was not properly modified to implement proper BigDecimal support.
Hope this helps! This really needs to be fixed ASAP due to the sneaky errors it can produce that are hard to notice! Thanks guys!
OS: Linux
Java Version: 11.0.11
Tomcat Version: 8.0.36
Lucee Version: 6.1.0.243
3 posts - 2 participants