Using intval to return numeric value at a certain length?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Using intval to return numeric value at a certain length?

Post by KeeganWolf »

I'm sure there's a simple way to do this, I'm guessing by using intval.

I need to take the value of a variable, e.g. '42', and turn it into a length of 5 characters before outputting to csv. e.g. '00042'
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Using intval to return numeric value at a certain length?

Post by Eran »

if you're looking to simply add zeros, check out str_pad()
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using intval to return numeric value at a certain length?

Post by requinix »

Or for a more numeric approach, sprintf.

Code: Select all

echo sprintf("%05u", 42);
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Re: Using intval to return numeric value at a certain length?

Post by KeeganWolf »

Thanks!
Post Reply