qodeninja
qodeninja

Reputation: 11266

How do you get number_format() to add two decimal points on integers less than 10?

The price is in cents, but whenever the price is less than $10 its not adding .00!

              $price = (float)($cents/100);
              $price = number_format($price,2);   

I want to be able to represent 0.00 and 0.01 and 1.01 not sure how to do this if number_format() doesnt work!

Upvotes: 1

Views: 3136

Answers (1)

miku
miku

Reputation: 188064

Take a look at money_format.

<?php

$prices = array(100, 10, 1, 0.10, 0.01);
foreach ($prices as $price) {
    echo money_format('%.2n', $price) . "\n";
}

// 100.00
// 10.00
// 1.00
// 0.10
// 0.01
?>

Upvotes: 9

Related Questions