scriptygoddess

27 May, 2003

When a number isn't a number…

Posted by: Jennifer In: Lessons learned

John (no URL provided) sent me a question based on some problems he was having with a javascript he had written. I've seen this happen with a few people now so I think a "tip" is in order. In javascript, ASP, and PHP(?) (and I believe other scripting and programming languages) – you can not have a number (ie. Integer, Float, etc.) with leading zeros. If you do – it will think it's something else (specifically a hexidecimal, or octal number.)

Here's a definition of hexidecimal and octal numbers I found here:

Hexadecimal and Octal numbers may also be represented in JavaScript. A hexadecimal number (base 16) is represented with a leading 0x, or 0X. The case of the X represents whether to use upper-case of lower-case letters for the hexadecimal number. Octal numbers are represented with a leading 0.

(don't ask me to explain hexadecimal or octal numbers more than that. I've never had to use them, and I don't know how I would. I only understand the textbook explanation you see above. LOL!)

If you have a number with leading zeros, and you need to KEEP the leading zeros, you need to treat it like a string. (i.e. put quotes around it).

So let's say you have a function that takes an ID number. Your ID number has leading zeros. (Lets say this function simply needs to take your number and forward you to a page with the ID number as the "name" of the page).

function fakeFunction(id) {
document.location= id + ".htm"
}
… some code …
<a href="#" onClick="fakeFunction(00123)">This link to page 00123 won't work</a>
but
<a href="#" onClick="fakeFunction('00123')">this one will</a>

If you want to see what it's reading (and this is a really good bugtesting tip in general) put in an alert. So make this the first line in your function:

alert(id)

In this example – the first link will pop up an alert that says "83" (I guess 00123 is the octal (?) equivalent to 83) the second will pop up the correct value; "00123".

Additionally – if you're populating a database – and the database is expecting a *NUMBER* you can't pass it a string. So if you've made your number with leading zeros into a string – you won't be able to put it into the database. If your "id" field is set to accept a number, this insert statment will give you errors:

INSERT into student (name, id) values ("Joe Schmo", "00123")

It needs to be this:

INSERT into student (name, id) values ("Joe Schmo", 123)

Yes, the leading zeros are gone. I suggest adding them in later (or changing your field type). You can easily add leading zeros to a number in php using the sprintf function like this

$aNumberThatsAString = sprintf("%05d"."id");

Breaking that down: %0 (pad with zeros) 5 (so that there are 5 "digits") d (this is an integer/decimal number)

Feel free to add to this or correct me in the comments. Some of the above may be incorrect assumptions…

3 Responses to "When a number isn't a number…"

1 | Paul

May 29th, 2003 at 11:37 am

Avatar

I ran across this same problem (I think it was asp) and discovered that when a zero was added in front of a number – say 1 – it actually promoted my int to a double. Is this an undocumented feature?

2 | Mike

May 29th, 2003 at 2:34 pm

Avatar

Just to round this out —

Normal, decimal (base 10) numbers work like we all learned – the rightmost digit is the ones, the next towards the left is the tens, then the hundreds, and so forth.

So, 156 is 6 ones, 5 tens, and 1 hundred.

To get to other bases, we've got to not think of that as ones, tens, and hundreds, but 10^0, 10^1, and 10^2. (That's ten to the zeroth power, ten to the first power, ten squared, and so on.) Ten to the zeroth is one, ten to the first is one, ten squared is ten times ten is one hundred, et cetera.

This is cool because we know the base is 10, and all the digits are 10 to a power. And, starting from the right, the power starts at 0 and increases by one.

Now enter binary, which is base 2. If we wanted a binary number, then instead of 10 to a power, we have 2 to a power. That makes the rightmost digit the ones (2^0), the next digit the "twos" (2^1), the next digit the "fours" (2^2), the next digit the "eights" (2^3), and so on.

The other catch with bases is that any particular digit can't be equal to higher than the base. So, in binary, each digit can only be a 0 or a 1. In decimal, each digit can only be 0 through 9. The reason is that, if I had 2 in binary, it's the same as 10 in binary. So, the digits only go up to 1. Decimal digits only go to 9, not ten.

So, let's take an example number in binary:
11010
This is:
No ones (2^0)
One two (2^1)
No fours (2^2)
One eight (2^3)
One sixteen (2^4)

Sixteen plus eight plus two is twenty-six, so 26 base 10 is the same as 11010 base 2.

See the pattern?

Octal is base 8. So, we'd have ones (8^0), eights (8^1), sixty-fours (8^2), et cetera. And, each digit goes from 0 to 7.

Example: 50 base 8 is five eights and no ones, which is forty in decimal.

What they've said about the leading zero is that the leading zero is the indication to the computer that you are giving it an octal, not decimal number. If I just saw:
25
then I wouldn't know if it were base 8 or base 10. By convention, that is a decimal (base 10) number, and if I fed it:
025
it knows the leading 0 says this is an octal number.

Hexadecimal works the same way, but in sixteens. Letters are used for ten and above; hexadecimal (abbreviated hex) digits are 0,1,2,3,…,8,9,A,B,C,D,E,F.

Example:
0x1A6 is one two-hundred-fifty-six (16^2), ten sixteens (16^1), and six ones (16^0). That makes, um, 422, I think. (Check my math on that.)

Now you get the joke behind one of my favorite blog names, 0xDECAFBAD. (Why couldn't *i* come up with something like that…)

3 | kain

January 5th, 2004 at 6:45 pm

Avatar

you saved me,
<a href='#' onclick=\"myimage_onclick('".$fuser->getVar("rf_clienti")."');\">".$fuser->getVar("rf_clienti")."</a>

I added the " to the function and now it return the correct value instead of a octal number, parseInt just stripped the zeroes,
thank tou.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements