How can I format a string from an input value '1' to '01' then typecasting it to string?
Thanks!
Printable View
How can I format a string from an input value '1' to '01' then typecasting it to string?
Thanks!
I am confused. It would always be a string. :confused:
PHP Code:$input = 1;
$input = '0' . $input; // it is a string
If you want the result to have two digits, even if the input has two digits, you can use sprintf:
Note that types aren't very important in PHP.Code:$string = sprintf("%02d", $input);
but what if the input has 3 or more?...
wouldnt it be better to always add a 0 to the input. or if 2 dont add zero. else add a zero
Then the result would still have 3 digits. Field width specifiers never truncate.Quote:
Originally Posted by PlaGuE
That would be far more complex, for no gain.Quote:
wouldnt it be better to always add a 0 to the input. or if 2 dont add zero. else add a zero