/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] parseInt() explaination

Could someone tell me where my error is in this? ?

[code]
<html>
<head>
<title>parseInt Test</title>
<script type=”text/javascript”>
var mm = ’08’;
var dd = ’09’;
document.write(‘<br>’+mm+’ : ‘+dd);
document.write(‘<br>’+parseInt(mm,16)+’ : ‘+parseInt(dd,16));
document.write(‘<br>’+parseInt(mm,10)+’ : ‘+parseInt(dd,10));
document.write(‘<br>All of above is correct for input given’);
document.write(‘<p>Below is un-expected’);
document.write(‘<br>’+parseInt(mm)+’ : ‘+parseInt(dd))
alert(‘What gives here???’);
</script>
</head>
<body>

</body>
</html>
[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledMar 20.2009 — obtained from Javascript: The Definitive Guide edition 4th. Core Javascript Reference:

Bugs

When no radix is specified, ECMAScript v3 allows an implementation to parse a string that begins with "0" (but not "0x" or "0X") as an octal or as a decimal number. To avoid this ambiguity, you should explicitly specify a radix or leave the radix unspecified only when you are sure that all numbers to be parsed will be decimal or hexadecimal numbers with the "0x" or "0X" prefix.[/quote]


reason you get zero is because in string '08', the eight isn't a digit in octal format. but more curious is, why parseInt wouldn't interpret the string as decimal when the string conform a digit not vaild on a format?
Copy linkTweet thisAlerts:
@slaughtersMar 20.2009 — The problem is the leading zero in the string. The zero in front is trying to tell the browser that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers.

Wrap a parseFloat around the string and then do a parseInt, or simply always specify base 10 when you know you will have strings with leading zeros.
Copy linkTweet thisAlerts:
@KorMar 20.2009 — In fact parseInt() is a method which converts a number from a certain base (0 to max 36, if I remember well) to a decimal number. People often forget that parseInt() [I]needs[/I] the radix in order to make clear from which base the number is converted. When the radix is missing, the interpreter takes the 'appropriate' base. I confess don't know either the reason for it prefers the octal and, in some cases, the base 6 instead of base 10 as default.
Copy linkTweet thisAlerts:
@KorMar 20.2009 — The problem is the leading zero in the string. The zero in front is trying to tell the browser that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers.

Wrap a parseFloat around the string and then do a parseInt, or simply always specify base 10 when you know you will have strings with leading zeros.[/QUOTE]


[B]Number()[/B] method is also a good one, when you are sure the number is decimal and it is not followed by letters or special characters.
Copy linkTweet thisAlerts:
@JMRKERauthorMar 20.2009 — Thanks to all of your for sharing the knowledge I seem to lack!

Much appreciated. ?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...