Converting a number into a 5 value string or array having the format "00000" [duplicate]

StackOverflow https://stackoverflow.com/questions/22543704

  •  18-06-2023
  •  | 
  •  

In PHP, I need to display a big 5 digit, graphical counter from a number fetched from a database. Examples:

  • If number = 1, counter should display 00001
  • If number = 15, counter should display 00015
  • if number = 999, counter should display 00999

What's the easiest way to achieve this?

有帮助吗?

解决方案 3

printf formats have a zero-padding option:

printf("%05d", $number);

其他提示

You could use str_pad:

$output = str_pad ($input, 5, '0', STR_PAD_LEFT);

The versatile printf or sprintf for the value 1 as 00001:

printf('%05d', 1);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top