Page 1 of 1

Using intval to return numeric value at a certain length?

Posted: Mon Aug 17, 2009 3:46 pm
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'

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

Posted: Mon Aug 17, 2009 3:51 pm
by Eran
if you're looking to simply add zeros, check out str_pad()

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

Posted: Mon Aug 17, 2009 4:00 pm
by requinix
Or for a more numeric approach, sprintf.

Code: Select all

echo sprintf("%05u", 42);

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

Posted: Mon Aug 17, 2009 4:07 pm
by KeeganWolf
Thanks!