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
5 Answers
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
.
-
@KonradRudolph If i have pass as
digit
value as integer that time given error, If pass as string that time not problem Commented Feb 1, 2018 at 11:45 -
1@HirenBhut No. I’m 100% sure that it works. The documentation says so. I even tested it just for you: gist.github.com/klmr/e1319f6d921a382e86296cce06eb7dbd Commented Feb 1, 2018 at 12:07
-
@KonradRudolph Please check this code gist.github.com/klmr/… Commented Feb 1, 2018 at 12:29
-
3@HirenBhut Well that’s completely different, and has nothing to do with
sprintf
. Check the format of integers, in particular the section about octal digits. Commented Feb 1, 2018 at 12:34 -
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 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";
-
1
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)
-
Great answer, the only thing that would make it definitive for benchmarking purposes would be a solution using
sprintf
and maybe also a while loop that prepends zeroes to the string, which is a pretty common implementation, especially in other languages. Commented Aug 5, 2024 at 20:38
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