Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding zeroes to Price

Tags:

php

$price = 10.00;
list($dollars, $cents) = explode('.', $price);
echo $dollars . '.' . $cents;

... almost works except that the zeros are omitted. 10.00 becomes 10 and 10.10 becomes 10.1

I see there's a padding function for strings, but anything for numbers or floats?

How do I fix this?

like image 806
eozzy Avatar asked Oct 15 '25 16:10

eozzy


2 Answers

You can use number_format:

echo number_format($price, 2); // Would print 10.00

You can specify a separator for the decimal point and another one for the thousands:

echo number_format(1234.56, 2, ',', ' '); // Would print 1 234,56
like image 155
Emanuil Rusev Avatar answered Oct 17 '25 05:10

Emanuil Rusev


Use Sprintf

$digit = sprintf("%02d", $digit);

For more information, refer to the documentation of sprintf.

like image 31
Venu Avatar answered Oct 17 '25 07:10

Venu