2

I have to create a JavaScript Number containing 17 decimal places. I used the following code :

var integerPart  = "10";
var roundedDigits = "12345678912345678";
var x  = Number(integerPart + '.' +  roundedDigits);

Output: 10.123456789123457

But when I try to output this number object, I get max of 15 decimal places. How can I achieve this?

Serlite
  • 12,130
  • 5
  • 38
  • 49
user2354566
  • 151
  • 1
  • 8
  • 2
    "Decimal" numbers are actually floats. Floats are an *imprecise* data type. Do you require any amount of precision of your values? Then you need to use strings. Only use floats if you don't mind values getting rounded or losing precision. – deceze Oct 07 '15 at 14:26
  • You should take a look at [Math.js](http://mathjs.org/) library, this is awesome to do what you want. – danilodeveloper Oct 07 '15 at 14:30
  • More precisely [here](http://mathjs.org/examples/bignumbers.js.html) – danilodeveloper Oct 07 '15 at 14:31

1 Answers1

0

all numbers in javascript are double. they have a maximum of 16 digits. you need a special datatype like BigDecimal

see this answer

Dealing with float precision in Javascript

and IEEE floating point

Community
  • 1
  • 1
lumos0815
  • 3,908
  • 2
  • 25
  • 25