0

I'm trying to get a decimal number without any decimal places limit in javascript. I have two numbers in my code, 610 and 987. If I divide them in the default windows calculator, I get the number 1.6180339850173579389731408733784. This number has 31 decimal places. If I divide them using javascript, it automatically rounds this number and limits it to 15 decimal places. Since I need this number to calculate further, it has to be the full decimal number, otherwise the final result is wrong.

Currently I'm using this code:

var multiplicator = 987/610; // returns 1.618032786885246
var maxloop = input - 15;

for(var i = 0; i < maxloop; i++){
    value *= multiplicator;
}

Since the variable multiplicator doesn't contain the whole decimal number, the value will be wrong for higher values, because of the rounding.

I already tried toFixed() function, but then I only get zeroes at the end of the decimal number instead of the correct decimal value and toFixed() allows only digits between 0 and 20.

var multiplicator = (987/610).toFixed(31); // wont work because toFixed() allows only digits between 0 and 20

Is there a way to get the full decimal number with all the 31 decimal places?

Therk
  • 391
  • 6
  • 23
  • Another one trapped by floating-point numbers – Jonas Wilms Jun 22 '17 at 09:57
  • The limit is a consequence of IEEE 754 maths. There's always [*BigDecimal*](http://stz-ida.de/index.php?option=com_content&id=18) and similar libraries. – RobG Jun 22 '17 at 09:59

0 Answers0