How to handle a number which has digits more than 17 in javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to handle a number which has digits more than 17 in javascript?

I have a number in json which is having digits more than 17. It does round off every time I try to show >17 digits. If 17 is the javascript limit. Please assist on how to handle this in code.

23rd Dec 2016, 5:58 PM
Nitesh Patil
Nitesh Patil - avatar
5 Answers
+ 6
The idea was to work with number stored as string, and convert only digit ( or few digits ) to integer so that you avoid problems of floating point numbers precision... It's relatively simple ( remember how you learn to calculate, on paper ) for integer, and with little more effort, if you need, you can handle floating point as well ;)
23rd Dec 2016, 8:39 PM
visph
visph - avatar
+ 3
@visph : I was wondering about custom handlers; if you followed the scheme of the current mantissa/exponent coding...but then: https://modernweb.com/what-every-javascript-developer-should-know-about-floating-points/ ...leads to BigDecimal, always using a tested library (preferred over writing your own) and arbitrary-precision floats: https://github.com/dtrebbien/BigDecimal.js
23rd Dec 2016, 7:45 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
You mean float precision. The maximum number of decimals is 17... http://www.w3schools.com/js/js_numbers.asp
23rd Dec 2016, 6:17 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
You can code an handler for it... Look at http://www.2ality.com/2012/07/large-integers.html?m=1 ( it's the first link google send me, and it seems to be a good point of start ^^ )
23rd Dec 2016, 6:42 PM
visph
visph - avatar
+ 2
@visph I got that :). And thanks, because other people can benefit from the tandem answers.
23rd Dec 2016, 8:43 PM
Kirk Schafer
Kirk Schafer - avatar