Node: num.toString() returning wrong number

Created on 17 Nov 2017  路  2Comments  路  Source: nodejs/node

when I run the number 361741715964559368 into .toString() I get "361741715964559360" back, the last number is 0 for some reason, same thing happens when i just do +""

I tried updating node, didnt fix the problem

question

Most helpful comment

361741715964559368 exceeds the range of integers that can be exactly described with a IEEE 754 binary64 number (in other words, a double), which is what JavaScript number type is, as it is greater than Number.MAX_SAFE_INTEGER or 9007199254740991. This is not a problem exclusive to Node.js, but is in fact part of the language itself (you can replicate this in all browsers).

The upcoming BigInt proposal would allow exact expression of this number. But for now, there is no way to deal with this imprecision, except using some userland third-party modules (1 2 3 4 5 6).

All 2 comments

361741715964559368 exceeds the range of integers that can be exactly described with a IEEE 754 binary64 number (in other words, a double), which is what JavaScript number type is, as it is greater than Number.MAX_SAFE_INTEGER or 9007199254740991. This is not a problem exclusive to Node.js, but is in fact part of the language itself (you can replicate this in all browsers).

The upcoming BigInt proposal would allow exact expression of this number. But for now, there is no way to deal with this imprecision, except using some userland third-party modules (1 2 3 4 5 6).

Answered, closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

willnwhite picture willnwhite  路  3Comments

jmichae3 picture jmichae3  路  3Comments

akdor1154 picture akdor1154  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

srl295 picture srl295  路  3Comments