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.
Related
This question already has answers here:
Javascript, why treated as octal
(6 answers)
Closed 7 years ago.
I am interesting in understanding why 032*2 returns 52 in JavaScript.
I have a feeling that 032 could be interpenetrated as octal notation but I could not find a proper reference to it.
Could you please:
Provide me an explanation with references.
Thanks in advance your help on this.
Numbers starting with 0 are Octal. So, 032 === 26.
To convert it into Base-10/Decimal number use parseInt with radix 10.
parseInt('032', 10) * 2; // 64
From MDN Docs:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
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.
If the input string begins with any other value, the radix is 10 (decimal).
ECMAScript 5.1 specifies some extensions to Numeric literals in its annex:
The syntax and semantics of 7.8.3 [Numeric literals] can be extended as follows except that this extension is not allowed for strict mode code:
NumericLiteral ::
DecimalLiteral
HexIntegerLiteral
OctalIntegerLiteral
OctalIntegerLiteral ::
0 OctalDigit
OctalIntegerLiteral OctalDigit
This concludes that any number which starts with a 0 and is followed by OctalDigits can be considered an octal one. However, this is not allowed in strict mode.
Octal interpretations with no radix
Although discouraged by ECMAScript 3 and forbidden by ECMAScript 5,
many implementations interpret a numeric string beginning with a leading 0 as octal.The following may have an octal result, or it may have a decimal result.
Always specify a radix to avoid this unreliable behavior.
Source: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/parseInt
This question already has answers here:
Prefix zero changes output in number addition [duplicate]
(2 answers)
Closed 9 years ago.
I have encountered a very weird issue with my JavaScript program. I have fount that JavaScript for some reason changes 040000 into 16384! [Example] Does anyone know why JavaScript is doing this?
It's because in js, number literals prepended with 0 are considered octal (base 8)
For example
010 == 8
In your example 040000 is really 4*8*8*8*8 = 16384 because in octal each 0 in the right multiplies the value by 8.
EDIT: Bonus:
If the leading 0 is in a string representation, (for example, if it was introduced by the user), and you want to avoid converting to octal, specify the base (aka radix) with value 10 in the parseInt method call, like this
var number = parseInt("040000", 10); //number will be 40000 ;)
In recent browsers, the radix is 10 by default, but not in old browsers, so if you want maximum compatibility also, always specify the radix parameter (usually 10).
Cheers
Because javascript thinks its in OCTAL format
Explanation:-
Javascript (like most programming languages) allows us to work directly with both octal and hexadecimal numbers, all we need is a way to tell which number base we are using when we specify a number. To identify octal and hexadecimal numbers we add something to the front of numbers using those bases to indicate which base we are using. A leading 0 on the front of a number indicates that the number following is octal while a leading 0x indicates a hexadecimal number. The decimal number 18 can therefore also be represented as 022 (in octal) and 0x12 (in hexadecimal). We don't put a special symbol on the front of decimal numbers so any number that doesn't start with 0 or 0x is assumed to be decimal.
So its same in your case
040000(base8)=16384(base10)
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
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.
Defaulting the radix to 8 (if the string starts with a 0) in JavaScript's parseInt function annoys me, only because I continue to forgot to pass the optional second argument as 10. I'm looking for an answer telling me why it makes sense to have it default to 8.
It only "defaults" to 8 if the input string starts with 0. This is an unfortunate carryover from C and C++.
You can use Number('0123') instead, or, as you said in the question, parseInt('0123', 10).
How do I work around JavaScript's parseInt octal behavior?
Can you tell me more about this carryover?
Javascript eval function returning Octal value
Octal number literals: When? Why? Ever?
Note: ECMAScript strict mode removes octal syntax.
If a number starts with 0 and contains digits between (and inclusive) 0 to 7, it is interpreted as an octal number (with base 8 instead of 10).
In parseInt however, if a string starts with a 0 it's always interpeted as an octal, and stops searching when it encounters an invalid character (e.g. the digits 8 or 9 or a character like z).
parseInt("070"); //56
parseInt("70"); //70
parseInt("070", 10); //70
parseInt("78"); //78
parseInt("078"); //7, because it stops before 8
If you need to convert a string into a number, and you're sure that it contains no invalid characters or fractional parts, you can multiply it with 1 to make a number of it:
1 * "070"; //70
I personally prefer this approach, and believe it's faster than calling functions.
Now, a couple of years later, parseInt() seems to work fine with numbers starting with 0. Current browsers:
parseInt("019"); // 19 on Firefox 67
parseInt("019"); // 19 on Chrome 75
parseInt("019"); // 19 on Safari 12
parseInt("019"); // 19 on IE 11
parseInt("019"); // 19 on Edge 42
But still, this "fix" must break older scripts that rely on parseInt("019") returning 1 or 0 instead of 19...