javascript problem with parseint [duplicate] - javascript

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)

Related

Why does a JavaScript function automatically convert a binary number when I pass it as an argument? [duplicate]

This question already has answers here:
Number with leading zero in JavaScript
(3 answers)
Closed 3 years ago.
I guess I can format it back. I'm just interested in why it's happening.
function test(d){
console.log(d) // 151028224
}
console.log(test(00001100101000))
By default, any number literally written with a zero at the beginning is considered as octal (base 8) number representation, and then, when you show back any number with console.log, it is written as its base 10 representation.
console.log(05)
console.log(06)
console.log(07)
console.log(010)
console.log(011)
It's recommended to avoid this in code, because it can lead to confusions :
if the number contains the digits 8 or 9, it cannot be a base-8 number, and thus treated as base 10 !
console.log(05)
console.log(06)
console.log(07)
console.log(08) // Yiiik !
console.log(09) // Yiiik !
console.log(010)
console.log(011)
The function has nothing to do with it.
The JavaScript compiler converts your number literal into a Number when it compiles the source code.
Since the number starts with a 0, it is treated as octal instead of decimal.

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.

javascript parseInt [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parseInt a string with leading 0
If I parseInt("01") in javascript its not the same as parseInt("1")???
start = getStartEugene("MN01");
start2 = getStartEugene("MN1");
getStartEugene: function(spot) //ex: GT01 GT1
{
var yard = spot.match(/[0-9]+/);
var yardCheck = parseInt(yard);
if (yardCheck < 10)
return "this"+yard;
else
return "this0"+yard
}
I want something to be returned as this+2 digits such as this25, this55, this01, this02, this09
But i am not getting it. Anyone know why?
You need to add the radix (2nd) argument to specify you are using a base 10 number system...
parseInt("01", 10); // 1
This happens because Javascript interprets numbers starting with zero as an octal (base 8) number. You can override this default behaviour by providing the base in which the string will be evaluated (as #jondavidjohn correctly pointed).
parseInt("10"); // returns 10
parseInt("010"); // returns 8

Bug in Javascript parseInt method? [duplicate]

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

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.

Categories