MikeMcl / decimal.js

An arbitrary-precision Decimal type for JavaScript
http://mikemcl.github.io/decimal.js
MIT License
6.36k stars 480 forks source link

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

Closed liubolp closed 3 years ago

liubolp commented 3 years ago

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

MikeMcl commented 3 years ago

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

liubolp commented 3 years ago

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?

MikeMcl commented 3 years ago

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.

liubolp commented 3 years ago

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