Aravind S
Aravind S

Reputation: 535

How to prevent large decimal numbers being rounded off in javascript

Say I have a huge number: 999999999.9999999999; When I try to store this number into a variable, in javascript, It stores the value 1000000000 into that variable.

How do I prevent this from rounding off and store the number as it is?

Upvotes: 1

Views: 592

Answers (1)

Sohail Ashraf
Sohail Ashraf

Reputation: 10569

You could use third party lib to handle this, like bignumber.js, you can read more about bignumber here

Please note that this is not possible with the javascript primitive types.

let x = new BigNumber("999999999.9999999999");
console.log(x.toString());
//Add example
console.log(x.plus(23).toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.0.0/bignumber.min.js"></script>

Upvotes: 1

Related Questions