...">

JavaScript - convert long number to string

"" + 237498237498273908472390847239084710298374901823749081237409273492374098273904872398471298374
> '2.3749823749827392e+92'

      

I am calculating IDs in a nice and secret way:

time = new Date().getTime()
pid = process.pid
host = 0; (host +=s.charCodeAt(0) for s in os.hostname())
counter = MIPS.unique_id()

"#{host}#{pid}#{time}#{counter}"

      

Unfortunately, somewhere along the way are identifiers (for example 11207648813339434616800

). Unfortunately, this means that they sometimes refer to 1.1207648813339434e+22

.

UPDATE

It seems to be a "bug / feature" of redis. never expected it.

# Bug with Big Numbers on zadd
redis = require 'redis'
r = redis.createClient()
r.zadd 'zset', '342490809809999998098', 'key', ->
    r.zscore 'zset', 'key', (_, results) ->
        console.log typeof results # string
        console.log results # 3.4249080981000002e+20

      

+3


source to share


2 answers


Your number is converted to 2.3749823749827392e+92

before you concatenate the number with a string to convert it.

The only solution is to use a container format that accepts an arbitrary number of digits, which is either a string or an array.



Can you provide us with some details on how you get this number?

+1


source


Javascript uses an 8-byte double to store large numbers, which is 53-bit precision. In your case, it is far from 53 bits, so you should use a large number library that can store large numbers accurately. Try javascript-bignum



+2


source







All Articles