Bug in Javascript parseInt method? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
Parsing a string using parseInt method returns invalid output .
Code :
parseInt("08");
Excepted Output :
8
Real Output :
0
Code [This returns output correctly] :
parseInt("8")
Output :
8
Why it happens ?

You need to specify the base:
parseInt("08",10); //=>8
Otherwise JavaScript doesn't know if you are in decimal, hexadecimal or binary.
(This is a best practise you should always use if you use parseInt.)

Also see Number:
Number("08"); // => 8
What is the difference between parseInt() and Number()?
What is the difference between parseInt(string) and Number(string) in JavaScript?

You should tell parseInt its 10 based:
parseInt("08", 10);
JavaScript parseInt() Function
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is
deprecated If the string begins with any other value, the radix is 10
(decimal)
http://jsfiddle.net/YChK5/

Strings with a leading zero are often interpreted as octal values. Since octal means, that only numbers from 0-7 have a meaning, "08" is converted to "0". Specify the base to fix this problem:
parseInt("08", 10); // base 10
As usual, the MDN is a good source of information:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt#Octal_Interpretations_with_No_Radix

Related

parseInt behaving different in IE and chrome [duplicate]

This question already has answers here:
How to parseInt a string with leading 0
(6 answers)
Closed 8 years ago.
i have parseInt function.
for 010 as input :
output in IE : 8
output in chrome and firefox: 10
for 099 as input:
output in IE : 0
output in chrome and firefox: 99
Could someone plz help me understand this different behaviour in the browsers..
issue is in IE and it is working as expected in chrome and firefox.
Thanks in advance.
Actually I would argue IE is doing things correctly* here . parseInt for a string starting with a 0 is treated as octal input.
To fix the problem, specify the base (in this case, you want decimal - 10):
num = parseInt(str, 10);
* In this case "correctly" really means "traditionally". The specification has actually changed.
Technically, a string starting with 0 can be treated as either octal OR decimal, depending on the implementor's whim, where decimal is now preferred:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

JS parseInt() returning 0 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why parseInt() works like this?
I have an issue with parseInt() returning 0 unexpectedly, here's a sample:
parseInt('-06') = -6
parseInt('-07') = -7
parseInt('-08') = 0
Why is the result 0? Same if I keep going down (-09, -10, ect). The format of the string comes from my framework so I need to deal with it. Thanks!
You need to pass a radix parameter when you use parseInt
parseInt('-08', 10);
When you don't, and when the string you're parsing has a leading zero, parseInt produces different results depending on your browser. The most common issue is that the string will be treated as a base-8 number, which is what you're seeing.
That's why this worked for '-06' and '-07'—those are both valid base-8 numbers. Since '-08' isn't a valid base-8 number, the parse failed, and 0 was returned.
From MDN
radix
An integer that represents the radix of the above mentioned
string. While this parameter is optional, always specify it to
eliminate reader confusion and to guarantee predictable behavior.
Different implementations produce different results when a radix is
not specified.
Also note that you can use the unary + operator to convert these strings to numbers:
​var str = '-08';
var num = +str;
console.log(num);​​​
//logs -8
DEMO
You could also try this:
'-06' * 1 = -6
'-07' * 1 = -7
'-08' * 1 = -8
this is a bug in firefox, use parseFloat instead .get more detaile about this bug here.
check parseFloat result HERE.

Problems with javascript "parseInt()" [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
How to parseInt a string with leading 0
document.write(parseInt("07"));
Produces "7"
document.write(parseInt("08"));
Produces "0"
This is producing problems for me (sorry for this babble, I have to or I can't submit the question). Anyone know why it's being stupid or if there is a better function?
If you argument begins with 0, it will be parsed as octal, and 08 is not a valid octal number. Provide a second argument 10 which specifies the radix - a base 10 number.
document.write(parseInt("08", 10));
use this modification
parseInt("08",10);
rules for parseInt(string, radix)
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
You want parseInt('08', 10) to tell it to parse as a decimal.
You just using input parameters of that function in a bit wrong way. Check this for more info. Basically :
The parseInt() function parses a string and returns an integer.
The radix parameter is used to specify which numeral system to be
used, for example, a radix of 16 (hexadecimal) indicates that the
number in the string should be parsed from a hexadecimal number to a
decimal number.
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is
deprecated
If the string begins with any other value, the radix is 10
(decimal)
So 07 and 08 is parsed into octal . That's why 07 is 7 and 08 is 0 (it is rounded to closest)
Try this :
parseInt('08', 10)
it will produce 8

alert(parseInt("09")); shows me "0" Why? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
EXTREMELY confused here.
parseInt("09") = 0
but
parseInt("9") = 9
Why is the prefixed zero not just stripped out?
alert(parseInt("01")); = 1
.. rage quit
Because that is treated as octal format, by default. If you want to get 9, you must add number base you want, and thats 10, not 8 (for octal), so call:
parseInt("09", 10);
A.7. parseInt
parseInt is a function that converts a string into an integer. It stops when it sees a nondigit, so
parseInt("16") and parseInt("16 tons") produce the same result. It would be nice if the
function somehow informed us about the extra text, but it doesn't.
If the first character of the string is 0, then the string is evaluated in base 8 instead of base 10. In base 8, 8 and
9 are not digits, so parseInt("08") and parseInt("09") produce 0 as their result. This error causes
problems in programs that parse dates and times. Fortunately, parseInt can take a radix parameter, so that
parseInt("08", 10) produces 8. I recommend that you always provide the radix parameter.
"JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc.,
978-0-596-51774-8."
this the reasons:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
http://www.w3schools.com/jsref/jsref_parseInt.asp
Do
parseInt("09", 10)
To specify base 10.
Edit: To clarify, a number prefixed by a zero will be assumed to be of octal notation, and "09" isn't a valid octal number.

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