'Zero-pad digits in string
I need to cast single figures (1 to 9) to (01 to 09). I can think of a way but its big and ugly and cumbersome. I'm sure there must be some concise way. Any Suggestions
Solution 1:[1]
First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that:
$s = sprintf('%02d', $digit);
For more information, refer to the documentation of sprintf.
Solution 2:[2]
There's also str_pad
<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6 , "___"); // produces "Alien_"
?>
Solution 3:[3]
Solution using str_pad:
str_pad($digit,2,'0',STR_PAD_LEFT);
Benchmark on php 5.3
Result str_pad : 0.286863088608
Result sprintf : 0.234171152115
Code:
$start = microtime(true);
for ($i=0;$i<100000;$i++) {
str_pad(9,2,'0',STR_PAD_LEFT);
str_pad(15,2,'0',STR_PAD_LEFT);
str_pad(100,2,'0',STR_PAD_LEFT);
}
$end = microtime(true);
echo "Result str_pad : ",($end-$start),"\n";
$start = microtime(true);
for ($i=0;$i<100000;$i++) {
sprintf("%02d", 9);
sprintf("%02d", 15);
sprintf("%02d", 100);
}
$end = microtime(true);
echo "Result sprintf : ",($end-$start),"\n";
Solution 4:[4]
The performance of str_pad heavily depends on the length of padding. For more consistent speed you can use str_repeat.
$padded_string = str_repeat("0", $length-strlen($number)) . $number;
Also use string value of the number for better performance.
$number = strval(123);
Tested on PHP 7.4
str_repeat: 0.086055040359497 (number: 123, padding: 1)
str_repeat: 0.085798978805542 (number: 123, padding: 3)
str_repeat: 0.085641145706177 (number: 123, padding: 10)
str_repeat: 0.091305017471313 (number: 123, padding: 100)
str_pad: 0.086184978485107 (number: 123, padding: 1)
str_pad: 0.096981048583984 (number: 123, padding: 3)
str_pad: 0.14874792098999 (number: 123, padding: 10)
str_pad: 0.85979700088501 (number: 123, padding: 100)
Solution 5:[5]
Here is my solution to handle both positive and negative numbers
<?php
// add zeros to a number at left or right side.
function add_zeros_to_number( $number, $number_of_zeros, $zeros_position="left"){
// check if number is negative
$is_negative = FALSE;
if ( strpos($number , '-') !== FALSE ){
$is_negative = TRUE;
$number = substr($number, 1);
}
if($zeros_position == "right"){
$r = str_pad($number, $number_of_zeros, "0", STR_PAD_RIGHT);
}else{
$r = str_pad($number, $number_of_zeros, "0", STR_PAD_LEFT);
}
if( $is_negative ){
return "-".$r;
}else{
return $r;
}
}
// how to use
$number = -333; // Desire number
$number_of_zeros = 4; // number of zeros [ your number length + zeros ]
$position = "right"; // left or right . default left
echo $result = add_zeros_to_number($number, $number_of_zeros, $position);
// output
// -333 => -3330 left
// -333 => -0333 right
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gras Double |
| Solution 2 | LeppyR64 |
| Solution 3 | Community |
| Solution 4 | |
| Solution 5 | user889030 |
