0

Hello my javascript code is below.

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.push(611087636212662272);
    document.getElementById("demo").innerHTML = fruits;
}
</script>

Output is

Banana,Orange,Apple,Mango,611087636212662300

I need the Output as:

Banana,Orange,Apple,Mango,611087636212662272

I entered "611087636212662272" then why does Javascript return "611087636212662300" as output.

How can I resolve this?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Renish Khunt
  • 5,620
  • 18
  • 55
  • 92
  • 3
    http://stackoverflow.com/questions/21278234/does-parsedouble-exist-in-javascript/21278297#21278297 - tldr; that is as exact a *number* as JavaScript can represent. The 'rounding' is due to limitations of floating point numbers (which all numbers in JavaScript are). The *literal* 611087636212662272 results in the *value* 611087636212662300 - try it: `611087636212662272 === 611087636212662300` is true. – user2864740 Jul 08 '15 at 06:38
  • 2
    You may find [this answer of mine](http://stackoverflow.com/questions/26228542/angular-js-parsing-integer-incorrectly/26228696#26228696) helpful. – Chris Hayes Jul 08 '15 at 06:38
  • 2
    What solution to choose to fix this depends on what you are using the number for (i.e. which operations you perform with it). – Felix Kling Jul 08 '15 at 06:41
  • This can help - http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin – sanket joshi Jul 08 '15 at 06:45

3 Answers3

0

Solution - add " in value, it will add exact value to fruits array.



    fruits.push("611087636212662272"); // this will push exact value

Unni Babu
  • 1,839
  • 12
  • 16
  • get a data form database and i have more then 5000 data it's not possible to convert from Integer to String. if i create a loop then it's take time. my application is already slow. – Renish Khunt Jul 08 '15 at 06:43
  • 2
    Well, the added value will be a string now, instead of a number. A possible solution, but it changes the way the value can be processed. – Felix Kling Jul 08 '15 at 06:43
0

The number is too big and lose precision. JavaScript Numbers are 64-bit floating point values, the largest exact integral value is 2^53, or 9007199254740992.

ECMA Section 8.5 - Numbers

Note that all the positive and negative integers whose magnitude is no greater than 2^53 are representable in the Number type (indeed, the integer 0 has two representations, +0 and −0).

  • 1
    Saying "the largest exact integral value is 2^53" is not correct. There *are* exact integral values larger than that - 611087636212662300 from the question is one of them. The important point is that not *all* integers larger than 2^53 are representable, and these gaps manifest themselves in apparent "rounding" of numbers, to nearby exactly-representable numbers. – Chris Hayes Jul 08 '15 at 07:11
0

this is another way to achive

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var fruitNum  = fruits + ' 611087636212662272';
document.getElementById("demo").innerHTML = fruitNum;
Danish Khan
  • 143
  • 1
  • 9