# Thread: How to make a number to 6 characters?

1. ## How to make a number to 6 characters?

Hi all,

How do i make a integer that I pass down to have 6 characters by adding 0 infront? Any help would be aprreciated. Thanks~

For example:
394->000394
2832->002832
52->000052

2. Do this:
PHP Code:
``` \$input = 1000;echo str_pad(\$input, 6, 0, STR_PAD_LEFT);  ```

3. Note: Should only be for display, not for internal use.

4. Originally Posted by logic_earth
Note: Should only be for display, not for internal use.
Yes login_earth, it would be nice to have the field (in case of database table - MySQL) zero fill. Am I right?

What you mean "not for internal use"?

5. You could but if you want to do things with that number like math when you convert it to integer the leading zeros are removed.

Or in some cases PHP thinks the number is octal which has a leading zero (mostly). Bad things happen.
But this is a rare problem mostly due to bugs in the system.

Really just best to leave the leading zeros for display only.

6. Ummm! the following code is working something strange:
Code php:
```\$input = "001000";
echo \$input * 2; # Gives 2000

\$input = 001000;
echo \$input * 2; # Gives 1024;

\$input = 1000;
echo \$input * 2; # Gives 2000```

Is the second one acting as octal?

7. Hmm well the octal of 001000 is 330 so cannot be that...hmmm
Only proves my point that leading zeros = unexpected behavior.

Interesting tho converting to an integer doesn't have this problem as the leading zeros are dropped.

8. Code php:
```\$input = 001000;
echo gettype(\$input);```

This gives result me "INTEGER" as value type though.

9. 001000 is a octal value which translates to decimal 512. When multiplied by 2 it becomes 1024. So no surprises up there.

10. Originally Posted by rajug
This gives result me "INTEGER" as value type though.
Off course it would. An octal whole number is an Integer. So is the Hex and decimal equivalent.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•