简体   繁体   中英

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!

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
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM