mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Multiplication by 100 returning unexpected results

Open rmclaughlin-nelnet opened this issue 1 year ago • 1 comments

The issues section is used only for bug reports. Please use the Discussions section to ask questions and share ideas and suggestions.

Describe the bug I am trying to convert a decimal representation of a percent to a integer. So I am multiplying the number by 100, but the result is not something I expected.

To Reproduce

const math = require('mathjs'); math.chain(0.0749).multiply(100).done(); 7.489999999999999

Any help in fixing this? I would round, but I don't always know how many decimals I need. I am currently using v 12.4.1

rmclaughlin-nelnet avatar May 02 '24 21:05 rmclaughlin-nelnet

This is the most asked question. It is a floating point round-off error. You can either use format to hide the round-off error in the output, or use BigNumber to work with a higher precision.

See docs: https://mathjs.org/docs/datatypes/numbers.html#roundoff-errors

josdejong avatar May 08 '24 07:05 josdejong

Closing as duplicate of #2811.

gwhitney avatar Jan 27 '25 03:01 gwhitney