MikeMcl/decimal.js

why Decimal('1.1249999999999999999').toNumber() is 1.125 and toFixed(2) is '1.12'

liubolp opened this issue · 4 comments

var a = new Decimal(25).div(24).mul(0.99)
var b = new Decimal(24).div(23)
var c = new Decimal(23).div(22)
var d = new Decimal(25).div(24).mul(0.99).mul(24).div(23).mul(23).div(22)
var e = a.mul(b).mul(c)

why d.equals(e) is false!!!

image

First, please explain to me how you set the precision of the division operation and what effect that has.

Thank you for replying to my question, all my configurations are default. Decimal.precision is 20.
I want e.toFixed(2)=='1.13' What should I do?

What do you think is the effect of having precision set to 20? Look at the result of individual division and multiplication operations. I am not going to do the work for you.

e.toDP(Decimail.precision - 1).toFixed(2)Solved my problem!
Thank you!