Code Monkey home page Code Monkey logo

Comments (4)

xfournet avatar xfournet commented on September 18, 2024 1

Just have to deal with that problematic, and I agree with @LesterLyu there is no need for big.js for such use case. You would need it only if you need number with more than 15 significant digits.

@or4 you should use toPrecision(15) instead of toFixed(15) so you can cut the number precision whatever the number of digit before or after the dot. See https://stackoverflow.com/a/36369703

Both JS and Excel use IEEE-754 double precision which can have 15 to 17 significant digits. While Excel retain 15 digits when representing number, JS can retain up to 17 digits with the risk of having inaccurate 16th & 17th digits. While it's better for computation, it's not desirable for representation where you should limit to 15 digits to have only accurate digits

Demo in JS, you can compare that toPrecision(15) works as in Excel (when formatting with enough decimal)

n = 0.2**-2;
console.log(n);
console.log(Number(n.toFixed(15)));
console.log(Number(n.toPrecision(15)));
console.log();

n = 0.1+0.2;
console.log(n);
console.log(Number(n.toFixed(15)));
console.log(Number(n.toPrecision(15)));
console.log();

n = 1234567890123451234;
console.log(n);
console.log(Number(n.toFixed(15)));
console.log(Number(n.toPrecision(15)));
console.log();
24.999999999999996
24.999999999999996
25

0.30000000000000004
0.3
0.3

1234567890123451100
1234567890123451100
1234567890123450000

@LesterLyu should the documentation updated with that information that can help people to deal with that ? And the demo too ?

from fast-formula-parser.

LesterLyu avatar LesterLyu commented on September 18, 2024

24.999999999996 should be fine. See link
If you want to display the number, please use SSF to format the value into number format.
SSF usage

from fast-formula-parser.

LesterLyu avatar LesterLyu commented on September 18, 2024

Also keep in mind big.js is super slow in calculation, which is not ideal.
See the benchmark.

from fast-formula-parser.

or4 avatar or4 commented on September 18, 2024

hi, gus, thanks a lot for answers
it is very useful, thanks again!

from fast-formula-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.