1

I am trying to pass a big int to a function from an onclick event in HTML. The ints are always very long, and I cannot seem to pass it to my function without rounding. I have tried some bigInt libraries to the same end, though I would much rather prefer simple string casting.

My js function:

    function initBuy(id){
      console.log(id.toString());
    }

and my HTML event:

<dt></dt><dd><a id="buy" onclick="initBuy(String(' + all_data[index].listing_id + '))" class="btn btn-success">Buy This Item</a></dd>

An example of a passed int:

13934317650292905813

and the result of clicking the button:

"13934317650292906000"

The passed int looks fine when I write it to an elements' text. When I pass it to a function, however, it's rounding it.

Matthew Darnell
  • 1,315
  • 2
  • 12
  • 24
  • Is that line with the HTML part of some server-side code? What is `all_data`? – nnnnnn Nov 23 '14 at 01:12
  • I just added the String() in the HTML because nothing else was working, and it didn't either. all_data is just a json array. – Matthew Darnell Nov 23 '14 at 01:13
  • There's no reason for `.listing_id` to be a number in the first place if it's just an ID. Represent it as a string in your JSON and in the `all_data` object. Don't use `String()`. – nnnnnn Nov 23 '14 at 01:15
  • 1
    I know the number is too big for JavaScript. However, I'm not asking how to expand JavaScripts max precision. If I set all_data[index].listing_id to an elements text, it works. I want to know why I can't convert it to a String. – Matthew Darnell Nov 23 '14 at 01:16
  • nnnnnn: I don't have control over the json – Matthew Darnell Nov 23 '14 at 01:20
  • If you don't have control over the source JSON and it has that value as a number then by the time you try to convert the property to a string it is too late because JavaScript is already representing the value as a JS number and the precision has been lost. You'd need to manipulate the JSON _before parsing it_ to change the numeric value to a string. (How complicated is the source JSON? You might be able to do a regex replace to add quotation marks around the numbers before parsing it.) – nnnnnn Nov 23 '14 at 02:06
  • @nnnnnn: but.. the OP clearly states he already *has* a string: `The passed int looks fine when I write it to an elements' text. When I pass it to a function, however, it's rounding it.` and in comment: `If I set all_data[index].listing_id to an elements text, it works.`.. If the "int" wasn't a string the OP *could not* write it successfully to the "elements' text".. Thus my answer. – GitaarLAB Nov 23 '14 at 04:01

2 Answers2

1

From the post here the maximum value an integer in Javascript could take is 9007199254740992

Your number 13934317650292905813 is far bigger than that.

From this post here you can use BigInteger.js to accommodate big integers

Community
  • 1
  • 1
Rama Kathare
  • 920
  • 9
  • 29
  • The mere act of passing string `'13934317650292905813'` to a function (as an argument) does not convert it to a (floating point) number; it should still be a string (until the OP (accidentally) converts it). – GitaarLAB Nov 23 '14 at 04:10
0

You say in your (ambiguous) question:

'The passed int looks fine when I write it to an elements' text. When I pass it to a function, however, it's rounding it.'

and in your comment:

If I set all_data[index].listing_id to an elements text, it works.

That means you are already getting the 'integer' as text-string in JSON.

Nothing in your current question converts the string to a number (I tested it).
As soon as the string would be converted to a number it would overflow IEEE 754's max accuracy of 2^53=9007199254740992.

Note that: initBuy(String(' + all_data[index].listing_id + '))
will return the string + all_data[index].listing_id + (as it should).

Passing the string '13934317650292905813' to your initBuy function also returns string '13934317650292905813' (as it should).

In other words, I can not reproduce your problem using the code you have supplied.

I assume you have simplified your initBuy function for this question (you'd have to post the original one for further examination, preferably together with an excerpt of a relevant part of the raw JSON string).

I assume you might accidentally convert the string to a number (probably using a +) inside that function!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80