Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with parseInt() - Alternatives? 1

Status
Not open for further replies.

daph

Programmer
Jun 9, 2000
65
CA
Hey everyone,<br><br>Here's the thing...I've used parseInt(data) to be able to do operations with the entries in specific input text boxes...It works great if the user writes the numbers correctly...but if the user enters a number with a zero in front of it (045), that number isn't used correctly...it'll be equal to 37...and 049 is equal 4 (??) I don't understand what's happening...I was wondering if there are other ways to evaluate caracters as numbers? <br><br>Hope I'm being clear...if not, let me know!<br><br>Thanks :)
 
try: Parseint(your_number, 10) <br>because if your number begins with a 0, parseInt thinks it's hexadecimal - you have to set the base
 
Using the variable name data as an example, we could also say:<br><br>data = new Number(data)<br>than use window.isNan(data) to be sure they only typed in numbers, parseInt can give the user the wrong idea cuz it will accept things like 1005snarfoofoo<br><br><A HREF="mailto:jared@aauser.com">jared@aauser.com</A>
 
maybe not SNarfOOfOO as s, n and o are not hexadecimal figures - but 1005arff would b accepted, yes ;)
 
small correction:&nbsp;&nbsp;if the number starts with a 0, parseInt treats it as octal, not hex.<br><br> <p>nick bulka<br><a href=mailto: > </a><br><a href= > </a><br>
 
Oh Nick... who gives a radix! hehe ;o)<br><br>Sorry, couldn't resist another stupid joke.<br><br>-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top