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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -