158

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

Braiam
  • 1
  • 11
  • 47
  • 78

5 Answers5

251

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.

Gras Double
  • 15,901
  • 8
  • 56
  • 54
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • @KonradRudolph If i have pass as `digit` value as integer that time given error, If pass as string that time not problem – Hiren Bhut Feb 01 '18 at 11:45
  • 1
    @HirenBhut No. I’m 100% sure that it works. The documentation says so. I even tested it just for you: https://gist.github.com/klmr/e1319f6d921a382e86296cce06eb7dbd – Konrad Rudolph Feb 01 '18 at 12:07
  • @KonradRudolph Please check this code https://gist.github.com/klmr/e1319f6d921a382e86296cce06eb7dbd#gistcomment-2338628 – Hiren Bhut Feb 01 '18 at 12:29
  • 3
    @HirenBhut Well that’s completely different, and *has nothing to do with `sprintf`*. Check the [format of integers](http://php.net/manual/en/language.types.integer.php), in particular the section about octal digits. – Konrad Rudolph Feb 01 '18 at 12:34
  • @KonradRudolph Yes, Can any possible solution ? – Hiren Bhut Feb 01 '18 at 12:36
  • @HirenBhut Err just write the integer value like you normally would: without a leading zero. – Konrad Rudolph Feb 01 '18 at 12:36
108

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_"
?>
LeppyR64
  • 5,251
  • 2
  • 30
  • 35
77

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";
Community
  • 1
  • 1
Alex
  • 1,522
  • 1
  • 15
  • 17
2

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)
Cray
  • 2,774
  • 7
  • 22
  • 32
0

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

user889030
  • 4,353
  • 3
  • 48
  • 51