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