47

I'm trying to generate invoice numbers. They should always be 4 numbers long, with leading zeros, for example :

  • 1 -> Invoice 0001
  • 10 -> Invoice 0010
  • 150 -> Invoice 0150

etc.

mike23
  • 1,312
  • 6
  • 20
  • 32
  • Possible duplicate? -- http://stackoverflow.com/questions/3963271/string-numbers-into-number-numbers-in-php – tplaner Jun 09 '11 at 17:32

7 Answers7

97

Use str_pad().

$invID = str_pad($invID, 4, '0', STR_PAD_LEFT);

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
  • I like this answer better than mine - I think both should work - this just seems to fit the problem better. – Dave Jun 09 '11 at 17:29
  • 2
    @Dave Using `printf` functions is an equally valid way to do it here, but since Invoice IDs won't be used as numbers (mathematically speaking), treating them as strings like this is simply another way to do it. If there were ever non-numeric Invoice IDs (e.g., "order 001F3"), then string it is. – Wiseguy Jun 09 '11 at 17:31
  • 1
    @Wiseguy, thanks that worked even better than printf because I had to add a prefix to the numbers also, like INV0023. – mike23 Jun 09 '11 at 17:42
  • @mike23 Glad to help, though I'm not sure why this would be any easier to add 'INV'. – Wiseguy Jun 09 '11 at 17:47
  • 1
    Ack, but strings can also be padded with `printf('INV%04s',$something);`. Then again: if the invoice if the only fixed width thing you need, this solution is indeed better. If you need more fields on a line to be fixed-width (writing to fixed width files etc.), `printf` would probably be a better solution. – Wrikken Jun 09 '11 at 17:57
  • @Wrikken Exactly. One assumption I had was that the invoice ID would be used standalone at this point, possibly to save to a DB column and whatnot. – Wiseguy Jun 09 '11 at 18:02
  • _If_ it was for a MySQL DB column, I'd have mentioned the `ZEROFILL` attribute :) – Wrikken Jun 09 '11 at 18:04
  • @Wrikken, you're right. At first I had tried `$inv = "INV" . printf('%04s',$num)`, which didn't work. That's why I chose Wiseguy's solution. But in the end it seems there are Many ways to do the same thing here. – mike23 Jun 10 '11 at 06:19
  • 1
    @mike23 Ah, I see. [`printf()`](http://php.net/printf) actually outputs the string (like `print` or `echo` does); [`sprintf()`](http://php.net/sprintf) works identically but _returns_ the string rather than prints it. You'd want `sprintf()`. – Wiseguy Jun 10 '11 at 12:47
  • @Wiseguy, thanks for the clarification, I'm pretty new to printf/sprint. – mike23 Jun 10 '11 at 12:56
  • 1
    @mike23 Everybody's new at some point. Glad to see you're learning quickly. :-) – Wiseguy Jun 10 '11 at 12:57
  • I got my solution. Thank You. – Nana Partykar Sep 08 '17 at 16:04
30

Use sprintf: http://php.net/function.sprintf

$number = 51;
$number = sprintf('%04d',$number);
print $number;
// outputs 0051


$number = 8051;
$number = sprintf('%04d',$number);
print $number;
// outputs 8051
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
12

Use (s)printf

printf('%04d',$number);
Wrikken
  • 69,272
  • 8
  • 97
  • 136
4

Try this:

$x = 1;
sprintf("%03d",$x);
echo $x;
Dave
  • 28,833
  • 23
  • 113
  • 183
4

printf() works fine if you are always printing something, but sprintf() gives you more flexibility. If you were to use this function, the $threshold would be 4.

/**
 * Add leading zeros to a number, if necessary
 *
 * @var int $value The number to add leading zeros
 * @var int $threshold Threshold for adding leading zeros (number of digits 
 *                     that will prevent the adding of additional zeros)
 * @return string
 */
function add_leading_zero($value, $threshold = 2) {
    return sprintf('%0' . $threshold . 's', $value);
}

add_leading_zero(1);      // 01
add_leading_zero(5);      // 05
add_leading_zero(100);    // 100
add_leading_zero(1);      // 001
add_leading_zero(5, 3);   // 005
add_leading_zero(100, 3); // 100
add_leading_zero(1, 7);   // 0000001
webjawns.com
  • 2,300
  • 2
  • 14
  • 34
3

Use the str_pad function

 //pad to left side of the input
$my_val=str_pad($num, 3, '0', STR_PAD_LEFT)

//pad to right side of the input
$my_val=str_pad($num, 3, '0', STR_PAD_RIGHT)

//pad to both side of the input
$my_val=str_pad($num, 3, '0', STR_PAD_BOTH)

where $num is your number

Sani Kamal
  • 1,208
  • 16
  • 26
1
while ( strlen($invoice_number) < 4 ) $invoice_num = '0' . $invoice_num;
GSerg
  • 76,472
  • 17
  • 159
  • 346
user769573
  • 19
  • 1
  • Doing this will just result in the zeros being stripped off the next time anything in PHP casts it to a int/float. Have a read of http://php.net/manual/en/language.types.type-juggling.php to see why this is a bad idea :) – SlyDave Jul 19 '18 at 15:12