Smaller number is greater than bigger number [duplicate] - javascript

This question already has answers here:
Why parseFloat in javascript returns string type for me?
(2 answers)
Closed 4 years ago.
In my JS, I'm doing a check to see if one number is greater than the other. I'm attaching the image of Chrome DevTools:
As you can see here, the code has made it inside the if statement. On the right, in the Watch, you can see amount = "3.00" and available = "261.60".
What would cause the smaller number to be greater than the bigger number?

You are are comparing strings. In the code you are calling parseFloat, which is the right idea, but it's followed by toFixed(). toFixed() returns a string.
console.log(typeof parseFloat("3.00").toFixed(2))
You need to make sure you're comparing numbers. An easy way is:
if(+amount > +available)
Alternatively, don't call toFixed() until it's time to display the number.

Related

Javascript rounding this number down 9703248444284653 [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 1 year ago.
I'm sure this has something to do with floating point math or something, but I need to out how to correctly interpret this number 9703248444284653. Javascript apparently doesn't like it
If I just do this.
var test = 9703248444284653
console.log(test)
The result is 9703248444284652, which is obviously not correct. Problem is that 9703248444284653 is an id that can't be changed. I have no idea what to do with this.
Your value is larger than Number.MAX_SAFE_INTEGER.
console.log(9703248444284653n > Number.MAX_SAFE_INTEGER);
You should treat your IDs as BigInt values. So you can should probably store the values as strings and then parse them as BigInt values if you need to perform any math on them.
const
bigIntNative = 9703248444284653n,
bigIntConstructed = BigInt('9703248444284653');
console.log(typeof bigIntNative === 'bigint' && bigIntNative === bigIntConstructed);
console.log(`${bigIntNative}`);

Adding two big numbers in javascript [duplicate]

This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I've been trying to add the following numbers using javascript;
76561197960265728 + 912447736
Sadly, because of rounding in javascript it will not get the correct number, I need the number as a string.
I've tried removing the last few digits using substr and then adding the two numbers together and then putting the two strings together, sadly this doesn't work if the first of the number is a 1.
function steamid(tradelink){
var numbers = parseInt(tradelink.split('?partner=')[1].split('&')[0]),
base = '76561197960265728';
var number = parseInt(base.substr(-(numbers.toString().length + 1))) + numbers;
steamid = //put back together
}
steamid('https://steamcommunity.com/tradeoffer/new/?partner=912447736&token=qJD0Oui2');
Expected:
76561198872713464
For doing operations with such big integers, you should use BigInt, it will correctly operate on integers bigger than 2ˆ53 (which is the largest size that normal Number can support on JS
const sum = BigInt(76561197960265728) + BigInt(912447736);
console.log(sum.toString());
Edit 2020: This API still not widely supported by some browsers, so you may need to have a polyfill, please check this library

javascript parseInt() failing? [duplicate]

This question already has an answer here:
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Closed 6 years ago.
n = "11004691915666669"
parseInt(n)
parseInt(n, 10)
Both results are: 11004691915666668
This is off by one. I tested some other values and they convert correctly. Is this a bug in the library or is there a better way to convert?
See this SO question for more details. Your value is beyond the maximum integer that Javascript can represent with full accuracy.
Under the hood Javascript doesn't understand integers, everything's floating point. You're hitting the reduced accuracy that happens when you get to very large numbers.
From the Javascript reference at MDN given here.
The integer range for a Number is defined as follows :
var biggestInt = 9007199254740992;
var smallestInt = -9007199254740992;
Which is why since your number exceeds the biggestInt value preset by Javascript, the parseInt method only returns the integer value that lies within the Integer range for a Number in Javascript as defined above.

String to number is wrong [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Javascript parseInt() with leading zeros
(7 answers)
Closed 7 years ago.
I'm trying to convert a string to number in Javascript, but here is my problem :
var string = "068999999501111443";
parseInt(string); // 68999999501111440
Number(string); // 68999999501111440
Why does this happen and how to fix it ?
Thanks
This is because the number is too large to be stored accurately. It is being stored as a floating point number, which can only store a certain amount of precision. Beyond it's maximum precision, you'll get what look like weird rounding errors.
You'll get similar effects for decimals with a large number of decimal places. This is more well known, as it tends to occur more often, but it's exactly the same effect that is happening here.

Why does parsing a large number, of string form, give a larger number? [duplicate]

This question already has answers here:
Why do JavaScript converts some number values automatically
(5 answers)
Closed 7 years ago.
See snippet:
var num = "9223372036854775808";
document.write(num);
document.write("<br>");
document.write(parseInt(num, 10));
On running the code snippet, the first write yields:
9223372036854775808
and the third write yields:
9223372036854776000
But, I am only parsing the number in string form to number. Why does it give a still larger number?
I thought it might have to do something with limits of storage capacity, but then, why would it yield a larger number if it could not store the smaller?
I read this question: Why parsing a very large number to integer return 1 and the parseInt doc but they did not help much.
So, Why does parsing a large number, of string form, return a larger number?
The first result is fine as it is treated as a string.
In the second one, it crosses the value of int which is +/- 9007199254740992 ie, the maximum value which parseint can parse is 9007199254740992 and since your value 9223372036854775808 is larger than the maximum value. it is giving some garbage value.
As commented correctly by blunderboy it seems that that the reason could be this
The numbers in Javascript are 64 bit "double" precision which follow the IEE754 floating point. Sothe largest positive whole number that can therefore be accurately represented is 2^53 and the rest of the remaining bits are reserved for the exponent.
If you check the ECMA specs this has been explained.

Categories

Resources