Strange result - floating point maths

Im getting strange result: 596.45 * 100 = 59645.00000000001

Version: Lucee
Java: 1.8.0_162 (Oracle Corporation) 64bit
OS: Linux (3.10.0-1160.71.1.el7.x86_64) 64bit

It’s expected since it’s all floating point under the hood.
While I do see ACF giving an integer, the only way to guarantee this (that I know of) is by using round().

Typing the following into a browser console gives the same result:


@mmishyn This was a known issue in lucee LDEV-646 Floating point arithm. inaccuracy is handled inconsistently.

Please use PrecisionEvaluate() for math operations with float
Note: the input must be as a string in PrecisionEvaluate();

PrecisionEvaluate("596.45 * 100");

in this case why 596.40 * 100 = 59640 ? integer

To be honest Im not follow why such result, it should be integer always, that is the main point why some companies “Stripe” use this to avoid decimal

but Thank you for your reply and reference to doc

Happy to share our solution:

	<cffunction name="stripeAmountToCents" access="private" returntype="numeric" output="false" hint="convert amount to cents for Stripe">
		<cfargument name="amount" type="numeric" required="true" />

		<cfreturn round(arguments.amount * 100) />

1 Like

Thanks, will use PrecisionEvaluate seems to work

1 Like