mum
mum

Reputation: 1645

How format number value in php?

I want format number as style '00000' ex:

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

How format number in php?

Upvotes: 0

Views: 96

Answers (3)

h2ooooooo
h2ooooooo

Reputation: 39550

sprintf:

$formattedValue = sprintf('%05s', $value);
  • 0 is the character to pad 5 times. s specifies that it's a string.

Upvotes: 1

olaurendeau
olaurendeau

Reputation: 669

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

$formattedValue = str_pad($value, 6, "0", STR_PAD_LEFT);

Upvotes: 3

sss999
sss999

Reputation: 528

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

Upvotes: 0

Related Questions