|
-
Jan 3rd, 2006, 07:50 PM
#1
Thread Starter
Addicted Member
Formating Strings
How can I format a string from an input value '1' to '01' then typecasting it to string?
Thanks!
-
Jan 4th, 2006, 07:24 AM
#2
Re: Formating Strings
I am confused. It would always be a string. 
PHP Code:
$input = 1;
$input = '0' . $input; // it is a string
-
Jan 4th, 2006, 08:00 AM
#3
Re: Formating Strings
If you want the result to have two digits, even if the input has two digits, you can use sprintf:
Code:
$string = sprintf("%02d", $input);
Note that types aren't very important in PHP.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 4th, 2006, 08:40 PM
#4
Hyperactive Member
Re: Formating Strings
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
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Jan 5th, 2006, 06:07 AM
#5
Re: Formating Strings
 Originally Posted by PlaGuE
but what if the input has 3 or more?...
Then the result would still have 3 digits. Field width specifiers never truncate.
wouldnt it be better to always add a 0 to the input. or if 2 dont add zero. else add a zero
That would be far more complex, for no gain.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|