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?

4
  • 3
    stackoverflow.com/questions/21278234/… - 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. Commented Jul 8, 2015 at 6:38
  • 2
    You may find this answer of mine helpful. Commented Jul 8, 2015 at 6: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). Commented Jul 8, 2015 at 6:41
  • This can help - stackoverflow.com/questions/307179/… Commented Jul 8, 2015 at 6:45

3 Answers 3

0

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


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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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 Comment

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.
0

this is another way to achive

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var fruitNum  = fruits + ' 611087636212662272';
document.getElementById("demo").innerHTML = fruitNum;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.