javascript - Angular JS inaccurate substraction of two decimal numbers -
in angular js application have 3 amounts in 1 object. in chrome browser debugger have following:
payment.amountforclosing = payment.amountremaining - payment.amountreserved;
payment.amountremaining has value of 3026.2
payment.amountreserved has value of 2478.4
after substraction payment.amountforclosing has value of 547.7999999999997, , 547.8 displayed. when user tries make payment closing, validation logic returns error indicating there not enough money make payment closing because of state presented above.
those amount values come c# webapi 2.0 backend, system.decimal types.
when dealing currency, worst thing can use floating point numbers, can see. because of way how floats stored, operations may provide incorrect results. thing correct multiplication/division power of 10.
best way store currency integer/biginteger, or whatever, , save e.g. 4 decimal places 12100 should represent 1.21
then can subtraction/multiplication of integer numbers , @ end divide power of 10.
my suggestion transform numbers integer before operation divide float.
Comments
Post a Comment