MikeMcl / bignumber.js

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

Is it possible to limit the decimals on scientific notation with toString? #273

Closed rafael-lua closed 4 years ago

rafael-lua commented 4 years ago

Hello. I want to do the following:

In the readme example, the toString() method returned "1.111222233334444555566e+21". Is it possible to limit the amount of decimals so the value shown would be "1.1112e+21" instead?

MikeMcl commented 4 years ago
let x = new BigNumber('1111222233334444555566');
console.log(x.toExponential(4));           // '1.1112e+21'
console.log(x.toPrecision(5));             // '1.1112e+21'
console.log(x.precision(5).toString());    // '1.1112e+21'
rafael-lua commented 4 years ago

@MikeMcl

Oh, thank you! And sorry for the obvious question.