How do I parse the following string? [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to parseInt a string with leading 0
Workarounds for JavaScript parseInt octal bug
How can I parse the "09" into number?
alert(parseInt("09"));
This returns me 0 ..Why is that and how do I fix this?

Specify the base as well:
alert(parseInt("09", 10)); // outputs 9

Related

Javascript Leading 0 on Integers Wrong Value [duplicate]

This question already has answers here:
Javascript 0 in beginning of number
(3 answers)
Closed 5 years ago.
Javascript leading 0 on integers getting wrong value on console.log.
Why I am getting like this ?
Code:
console.log(456);
console.log(0456);
Output:
456
302
Because JS "translates" 0456 as an octal value, since it has a trailing zero and all its digits < 8.

In javascript parsing a string gives me huge number, why? [duplicate]

This question already has answers here:
How to convert a String containing Scientific Notation to correct Javascript number format
(8 answers)
Closed 5 years ago.
I have a string in response "2.222222522879E12",
When I do
parseFloat("2.222222522879E12")
2222222522879
But the expected value should be 2.22,
Whats happening ?
And how can I fix it
You're going to get 2.222222522879* 10^12 with that expression as E means exponent, or to the power of
As per your edit asking how to fix:
use the Number.prototype.toPrecision() function. Here is the documentation

How does Javascript get this value? [duplicate]

This question already has answers here:
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 5 years ago.
We're having a discussion in the office about how the hell this math works in JavaScript.
There was an instance where we were multiplying by 010 instead of 10 and this gave the incorrect returned value.
For example...
25.25 * 010 = 202
25.25 * 10 = 252.5 as expected
whats even weirder is if you do parseFloat(010) it gives you 8!
For 010 is decimal 8, so it get 202.
console.log(25.25 * 010);
Look at this answer for Java: Why "010" equals 8?. JavaScript has to do the same.
The answer is, that an octal number starts with a leading zero 0.

Why does "10" > "9" = false? [duplicate]

This question already has answers here:
Why is one string greater than the other when comparing strings in JavaScript?
(5 answers)
Javascript string/integer comparisons
(9 answers)
Closed 8 years ago.
Is this a failure in JavaScript's attempt to convert them to numbers? If so, what numbers are they being converted to? Or what is the logic behind the string 10 being less than the string 9?
It's comparing the strings "alphabetically", and 1 comes before 9 in the character "alphabet".

javascript problem with parseint [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
I am trying to parse an integer number.
a = parseInt("0005") <- gives 5
a = parseInt("0008") <- gives 0
Can someone explain what's happening? It doesn't make any sense to me.
When parseInt has a leading 0 and a radix parameter isn't specified, it assumes you want to convert the number to octal. Instead you should always specify a radix parameter like so:
a = parseInt("0008", 10) // => 8
Numbers starting with 0 are parsed as octal by parseInt, unless you specify a radix to use.
You can force parseInt to parse as decimal by doing
a = parseInt("0008", 10)

Categories