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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parseInt rounding? truncation? .net 2.0 1

Status
Not open for further replies.

adamroof

Programmer
Nov 5, 2003
1,107
US
ran into this prob recently. I think it was working fine, but moved to .net 2.0 and things just arent the same.

the initial "number" is pulled from a substring.

01 to 07 work fine, returning the 1 to 7, but 08 and 09 return as 0's

why!?

function testStuff()
{
var nPos = "01";
var total = parseInt(nPos) + 1;

var nPos2 = "09";
var total2 = parseInt(nPos2) + 1;

alert(total + " : " + total2);
}
 
Any number that starts with a zero is considered octal, as in many other C-based programming languages. This means you can have 0-7 as digits ONLY, and anything else means the number is not valid. Since parseInt() evaluates a string until an invalid character is found, processing any number string with an 8 or 9 will result in only the digits before 8 or 9 being returned. You can use parseInt(numberstring, 10) to force evaluation of a number as decimal (base 10).

Lee
 
thanks for the indepth explanation

works dandy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top