#33374 [Opn->Csd]: strval does not let me convert numbers to 'zero filled' strings
| From: | sniper@php.net | Date: | Mon, 10 Oct 2005 05:53:01 +0000 |
| Subject: | #33374 [Opn->Csd]: strval does not let me convert numbers to 'zero filled' strings | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-86311@lists.php.net to get a copy of this message | ||
ID: 33374
Updated by: sniper@php.net
Reported By: tiago dot freire at gmail dot com
-Status: Open
+Status: Closed
Bug Type: Feature/Change Request
Operating System: GNU/Linux
PHP Version: 5.0.4
Previous Comments:
------------------------------------------------------------------------
[2005-06-17 09:25:50] nickj-phpbugs at nickj dot org
Please see str_pad, as this already does what you're looking for:
http://php.net/str-pad
Example code:
for ($i=1;$i<=101;$i++) {
// This gives 'foo0001'
print "foo" . str_pad($i, 4, "0", STR_PAD_LEFT) . "\n";
}
------------------------------------------------------------------------
[2005-06-17 02:26:17] tiago dot freire at gmail dot com
Description:
------------
Suppose I want to build a string with fixed length (for whatever
reason) by appending a number to a string.
//I want strings like 'foo0001','foo0002','foo0010' etc
for ($i=1;$i<1000;$i++)
{
$string = 'foo'.$i;
}
strval could be useful IF it accepted a second (int) parameter to tell
it:
"This number will have <int parameter> characters in string
form,fill it to the left with zeroes"
With this behaviour,this would work:
for ($i=1;$i<1000;$i++)
{
$string = 'foo'.strval($i,4);//This would give me '0001'
}
What about floats? Another parameter.
Explained by example:
for ($i=1;$i<1000;$i++)
{
$string = 'foo'.strval($i,4,3);//This would give me '0001.000'
}
For completeness:
$number = 123456789;
$string = 'bignumber: '.strval($number,5);
Will result in the string 'bignumber123456789', since I asked for less
characters than the number has.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=33374&edit=1