how I should avoid decimal rounding in javascript suppose I have input like
input : 99999999999.999999
Expected output : 99999999999.999999
but i am getting output like
100000000000
here, internally javascript rounding the decimal place. please let me know how should I avoid this javascript rounding???.
For example:
var test = 99999999999.999999;
console.log(test); //This will print 100000000000 instead
As @Aliendroid suggested, you can use the BigDecimal.js library. In my perspective, this is the best way to handle double and floats in JavaScript
To answer to your question more specifically, you can actually do this:
var test_number = new BigDecimal('99999999999.999999');
console.log(test_number.setScale(6).toString());
and to your original question:
please let me know how should I avoid this javascript rounding???
Well you can't as there is not double number in JavaScript, only float. So for example if you do something like this:
console.log(99999999999.99); //This is a float number
This will actually output: 99999999999.99
It is NOT Rounding
It is floating point error
learn floating point number in programming !
use string if needed and some Big number library
Ref:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.