Reputation: 3875
I have the following numbers: 12.00 12.45
I want those to convert to 000000001200 000000001245
What is the best way to do that?
The total is 12 characters, the last 2 digit is for 2 digits after the decimal point.
Upvotes: 2
Views: 338
Reputation: 12860
You can use number_format and printf to remove any decimals and have zero padding. I included it in a function below:
<?php
function twelvedigits($a){
$a = number_format($a, 2, '', '');
printf("%012s\n", $a);
}
$one = 12.45;
$two = 12.00;
echo '$one: ';
twelvedigits($one);
echo '$two: ';
twelvedigits($two);
?>
See the example here: http://ideone.com/I3UOmQ
Upvotes: 1
Reputation: 132001
The "it simply works"-solution
echo str_pad((int) ($value * 100), '0', 12, \STR_PAD_LEFT);
You can look at sprintf()
for more techically solutions.
Upvotes: 5