Reputation: 23
Using PHP I want to format a number(7) like in 6 digits 000007
I had done this via printf
Function but wanted to store it in a variable how can I do so.
Below mentioned code not working, giving output as :000007 and printing it on screen:
$gensubjectid=printf("%06d\n", $origionalsubjectid);
Suggest.
Upvotes: 1
Views: 598
Reputation: 1840
Use str_pad().
$invID = str_pad($invID, 10, '0', STR_PAD_LEFT);
Or
Use sprintf: http://php.net/function.sprintf
$number = 51;
$number = sprintf('%10d',$number);
print $number;
// outputs 0000000051
$number = 8051; $number = sprintf('%10d',$number); print $number; // outputs 0000008051
Upvotes: 0
Reputation: 10239
You can use: spirntf
$unformattedNumber = 7;
$formattedNumber = sprintf('%06d', $unformattedNumber);
Or you can try this (str_pad):
$input = 7;
str_pad($input, 6, "0", STR_PAD_LEFT);
Upvotes: 2
Reputation: 324820
Use sprintf
- it's identical to printf
but it returns the formatted string.
Upvotes: 7