0

I want format number as style '00000' ex:

$a=1 => $a2="00001"
$b=23 => $b2="00023"

How format number in php?

mum
  • 1,637
  • 11
  • 34
  • 58
  • you can use `str_pad` function, http://www.php.net/manual/en/function.str-pad.php – CodeBird Apr 12 '14 at 10:33
  • Does this answer your question? [Zero-pad digits in string](https://stackoverflow.com/questions/324358/zero-pad-digits-in-string) – Syscall Jan 16 '22 at 09:10

3 Answers3

3

You should use str_pad : https://www.php.net/manual/en/function.str-pad.php

$formattedValue = str_pad($value, 6, "0", STR_PAD_LEFT);
Syscall
  • 19,327
  • 10
  • 37
  • 52
olaurendeau
  • 659
  • 4
  • 12
1

sprintf:

$formattedValue = sprintf('%05s', $value);
  • 0 is the character to pad 5 times. s specifies that it's a string.
Syscall
  • 19,327
  • 10
  • 37
  • 52
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

use the str_pad function:- $a2 = str_pad($a, 5, "0", STR_PAD_LEFT);

sss999
  • 528
  • 3
  • 14