MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.63k stars 743 forks source link

Different result between toFixed and toNumber #364

Closed chend25 closed 7 months ago

chend25 commented 7 months ago
const result = BigNumber(3.15)
  .times(BigNumber(1).div(3));
const res1 = result.toFixed(); // '1.0499999999999999999895'
const res2 = result.toNumber(); // 1.05
const res3 = result.toFixed(1); // '1.0'
const res4 = BigNumber(res1).toFixed(1); // '1.0'
const res5 = BigNumber(res2).toFixed(1); // '1.1'

When res1 and res2 above are rounded to one decimal place, the results are different. By manual calculation, we know that 1.05 is the correct result I would expect res1 and res2 to have the same result when rounded to one decimal place

MikeMcl commented 7 months ago

I would expect res1 and res2 to have the same result when rounded to one decimal place

Why?

I don't see anything unexpected. Notice that BigNumber(1).div(3) is not the same as a third.