parseInt changes the integer - javascript

I am trying to pull a number (72157648141531978), which starts at the 21st character, out of the title of a page like so:
parseInt(document.title.substring(21), 10);
This returns the string as an integer of 72157648141531980. I can't seem to figure out why it is changing the last two numbers. Any help would be appreciated.

According to What is JavaScript's highest integer value that a Number can go to without losing precision? the max value of an integer is 9007199254740992.
I tried your calculation on http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_parseint and I can confirm your problem.
It looks like an issue parsing beyond this max value and it is rounding the last 2 figures.

You have exceeded the limits of double-precision floating-point format, as used by JavaScript. You cannot use that precise number directly in JavaScript. You can use it as a string, but if you need to do arithmetic on it you will need a bignum library.

Related

Converting big number string to number

What way i can convert string with 16 digits and 2 fraction value to number ?
Currently when I try to convert Number('1234567890123456.12') will became to 1234567890123456. fraction values will be gone.
I just want to confirm without using any third party lib can i convert this string to number ?
Unfortunately not. Javascript represents it's numbers using double precision floating point numbers. At 16 digits, it will only be able to store the integer component and not the part after the decimal point. You will need a bignum library to use this value.
EDIT: for reference the biggest integer you can use in JavaScript is 9,007,199,254,740,991
EDIT2: Thanks to Jeremy you can use a library like bignumberJS.
Your number has too many algorisms, I've created an example that simulates in the first position of the array the maximum length possible in javascript.
var nums = [
"12345678910111.12",
"1.5323",
"-42.7789"
];
nums.forEach(function(n) {
console.log(parseFloat(n).toFixed(2));
});
https://jsfiddle.net/7zzz1qzt/
I have faced issue to convert 18 digit string number to number. It is convert all digit to 0 after 16 digit. I have apply below code. it is working fine for me.
[{"id":${id},"name":"${name}"}]

Bug with outputting long numbers in javascript

Here is the code:
var q = 10000000000000011;
console.log(q);
Output will be:
10000000000000012
If I try to output 10000000000000010 or 10000000000000012, everything is fine.
Conversion to string doesn't help either.
How can I avoid this bug?
The maximum safe integer in JavaScript (2^53 - 1). which is 9007199254740991. You will need to use a big integer library to store such large numbers
As has been pointed out, the maximum integer size in JS is Number.MAX_SAFE_INTEGER (9007199254740991)
So if you want larger numbers, you'll have to get creative and use something like scientific notation:
10000000000000011 ~= 1 × 10^9 (1e9)

Force decimal value, even on integers, in Javascript

I need to take a user-input value and force it to 6 decimal places, even if the value is an integer. For example, the user types in 12, I need to convert that to 12.000000, as a number. This is not for display purposes - the system on the other end of my app requires decimal values, and there's nothing I can do about that.
As I've read elsewhere, numbers in Javascript are all 64-bit floating point numbers, so it doesn't seem like this should be so difficult.
Alas, toFixed is not an option here because that gives me a string value '12.000000'. Every other trick I've tried just yields the integer 12 with no decimal zeroes (e.g. wrapping toFixed with Number, dividing the string value by 1, and other such silliness).
Is it possible to represent an integer as a float in Javascript, without ending up with a string value?
UPDATE
Thanks for all the comments and answers. Unfortunately for me, #Enzey's comment actually answers my core question when he said that forcing precision can only be done with a string. If he submits that as an answer I'll accept it. I kept the details of my implementation purposefully vague because I didn't want to get into why I wanted to do what I'm doing, I just wanted to know if it was possible. But I guess I just ended up confusing people. Sorry about that.
Alas, there is no such thing as float or int in JavaScript. You only have Number, which does not have the slightest clue about a difference between 12 and 12.000000.
If you're sending it as a stringified JSON, you can use .toFixed on the number, and then strip the " signs from the numbers in the stringified JSON:
var result = JSON.stringify({
number: (12).toFixed(6)
})
.replace(/"[\d]+\.\d{6}"/g, function(v) {
return v.replace(/"/g, '');
});
console.log(result);

Angular JS inaccurate substraction of two decimal numbers

In my angular JS application I have three amounts in one object. In Chrome browser debugger i have following:
payment.amountForClosing = payment.amountRemaining - payment.amountReserved;
payment.amountRemaining has a value of 3026.2
payment.amountReserved has a value of 2478.4
after substraction payment.amountForClosing has a value of 547.7999999999997, and 547.8 is displayed.
When user tries to make another payment closing, my validation logic returns an error indicating that there is not enough money to make payment closing because of state presented above.
Those amount values come from C# WebApi 2.0 backend, as System.Decimal types.
When dealing with currency, worst thing that you can do is use floating point numbers, as you can see. Because of way how floats are stored, some operations may provide incorrect results. Only thing that will always be correct is multiplication/division by power of 10.
Best way is to store currency as Integer/BigInteger, or whatever, and save it with e.g. 4 decimal places so 12100 should represent 1.21
Then you can do subtraction/multiplication of integer numbers and at the end divide by power of 10.
My suggestion is that you transform those numbers to integer before any operation then divide it back to float.
Floating point precision in Javascript has been discussed before (eg. here or here). You have the following options:
Convert all your numbers to integers.
Format the result to a fixed number of significant digits using .toFixed(2)
Use a special datatype for decimals like BigDecimal
Out of all of these I agree with #PerunSS that in your case, the best option would be converting the numbers to integers before any operation, and the converting them back.

Javascript can't render this number correctly: 3494793310847464221

I have an interesting question, I have been doing some work with javascript and a database ID came out as "3494793310847464221", now this is being entered into javascript as a number yet it is using the number as a different value, both when output to an alert and when being passed to another javascript function.
Here is some example code to show the error to its fullest.
<html><head><script language="javascript">alert( 3494793310847464221);
var rar = 3494793310847464221;
alert(rar);
</script></head></html>
This has completly baffeled me and for once google is not my friend...
btw the number is 179 more then the number there...
Your number is larger than the maximum allowed integer value in javascript (2^53). This has previously been covered by What is JavaScript's highest integer value that a Number can go to without losing precision?
In JavaScript, all numbers (even integral ones) are stored as IEEE-754 floating-point numbers. However, FPs have limited "precision" (see the Wikipedia article for more info), so your number isn't able to be represented exactly.
You will need to either store your number as a string or use some other "bignum" approach (unfortunately, I don't know of any JS bignum libraries off the top of my head).
Edit: After doing a little digging, it doesn't seem as if there's been a lot of work done in the way of JavaScript bignum libraries. In fact, the only bignum implementation of any kind that I was able to find is Edward Martin's JavaScript High Precision Calculator.
Use a string instead.
179 more is one way to look at it. Another way is, after the first 16 digits, any further digit is 0. I don't know the details, but it looks like your variable only stores up to 16 digits.
That number exceeds (2^31)-1, and that's the problem; javascript uses 32-bit signed integers (meaning, a range from –2,147,483,648 to 2,147,483,647). Your best choice is to use strings, and create functions to manipulate the strings as numbers.
I wouldn't be all too surprised, if there already was a library that does what you need.
One possible solution is to use a BigInt library such as: http://www.leemon.com/crypto/BigInt.html
This will allow you to store integers of arbitrary precision, but it will not be as fast as standard arithmetic.
Since it's to big to be stored as int, it's converted to float. In JavaScript ther is no explicit integer and float types, there's only universal Number type.
"Can't increment and decrement a string easily..."
Really?
function incr_num(x) {
var lastdigit=Number(x.charAt(x.length-1));
if (lastdigit!=9) return (x.substring(0,x.length-1))+""+(lastdigit+1);
if (x=="9") return "10";
return incr_num(x.substring(0,x.length-1))+"0";
}
function decr_num(x) {
if(x=="0") return "(error: cannot decrement zero)";
var lastdigit=Number(x.charAt(x.length-1));
if (lastdigit!=0) return (x.substring(0,x.length-1))+""+(lastdigit-1);
if (x=="10") return "9"; // delete this line if you like leading zero
return decr_num(x.substring(0,x.length-1))+"9";
}
Just guessing, but perhaps the number is stored as a floating type, and the difference might be because of some rounding error. If that is the case it might work correctly if you use another interpreter (browser, or whatever you are running it in)

Categories