javascript parseInt() failing? [duplicate] - javascript

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.

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}`);

String conversion to integer [duplicate]

This question already has answers here:
How to convert a String to long in javascript?
(2 answers)
Closed 4 years ago.
I am trying to convert this "9876543210223023" into integer which is received as a string .I have used parseInt but it gets converted into 9876543210223024.
This is causing failure in my validations .
Please suggest how will the value remain same after conversion.
9876543210223023 is > than 9007199254740991, which is Number.MAX_SAFE_INTEGER.
JavaScript has a Number type, which internally is a 64 bit floating point number.
If the range is too big, the machine episilon is too large, and the number is rounded to the nearest representable number, in this case 9876543210223024.
You need a biginteger library, if you want to process numbers of this size.
If your browser is modern enough (aka Chrome/Chromium), it might have the type "BigInteger" already built-in.
In that case: BigInt("9876543210223023", 10)
Otherwise, the linked BigInt-library will act as polyfill.

Smaller number is greater than bigger number [duplicate]

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.

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.

Get a random number between 0.0200 and 0.120 (float numbers) [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 7 years ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I need to create a random number between 0.0200 and 0.120 using JavaScript. How would I do this?
I specifically am interested in floating point numbers, not whole numbers. The question linked (Generating random whole numbers in JavaScript in a specific range) specifically asks for "Generating random whole numbers", so even if some of the answers to the linked question specify how to do it for floats, it is not clear that is what they are doing. This question is clearly looking for a random number between floats and so is not a duplicate.
You could use
(Math.random() * (0.120 - 0.0200) + 0.0200).toFixed(4)
toFixed(n) is used to convert a number into a string, keeping only the "n" decimals.
Here you are:
function generateRandomNumber() {
var min = 0.0200,
max = 0.120,
highlightedNumber = Math.random() * (max - min) + min;
alert(highlightedNumber);
};
generateRandomNumber();
I understand your real question — you do not know how to get a random number between floating numbers, right? By the way, the answer is passed before.
To play with the code, just click here to jsFiddle.
Update
To get the four first numbers of your decimal, use .toFixed(3) method. I've performed an example here, on jsFiddle.
If you're looking to generate that and other random numbers or things, I'd suggest taking a look the Chance library. It provides a nice abstraction layer so you don't have to fiddle with Math.random() and write your own.
chance.floating({min: 0.02, max: 0.12});
Full disclosure: I'm the author so I'm a bit biased :)
Also, if this is the only random thing you need to generate or it's client-side where size is a real issue, I'd suggest just using one of the suggestions above. Not worth a few Kb where a couple lines will do.

Categories

Resources