Skip to content
Advertisement

Number formatting (Money) in PHP

I’m trying to format numbers with not decimal points or commas, I’ve tried number_format() and money_format() and can’t seem to get the result I need.


number_format($item->amount,2)

Result: 14,995.00


money_format(“%i”, $item->amount)

Result: 14,995.00


I’m want to get the following numbers formatted correct.

14995 needs to be £149.95

6795 needs to be £67.95

What is the best way to get the result above?

Advertisement

Answer

Using brick/money (disclaimer: I’m the author):

use BrickMoneyMoney;

// Instantiating from a decimal amount

$money = Money::of('67.95', 'GBP');
echo $money->formatTo('en_GB'); // £67.95

// Instantiating from a minor amount (cents)

$money = Money::ofMinor(6795, 'GBP');
echo $money->formatTo('en_GB'); // £67.95
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement