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

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

  •  18-06-2023
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione 3

printf formats have a zero-padding option:

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

Altri suggerimenti

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top