7

I want to format some numbers from my analytics into currency using the GB money format but as its for a dashboard and doesn't need to be that precise so I want to remove the pence (numbers after the decimal place) and round it which helps with the css layout, how do I do this? Rounding up or down to the nearest pound would be fine.

My code is as follows:

//set locale for currency
setlocale(LC_MONETARY, 'en_GB');

$sales = '8932.83';

echo utf8_encode(money_format('%n', $sales));

This outputs: £8,932.83

However how do I round this to be output as just £8,932 without anything after the decimal place.

I want to use currency format as sometimes the figure is negative in which case money_format returns the number like -£8,932.83 which is preferable to £-8,932 (pound and negative symbol around the wrong way) which is what happened when I formatted using number_format like so:

echo '£'.number_format($sales, 0, '', ',');
Ben Paton
  • 1,432
  • 9
  • 35
  • 59
  • Are you wanting something that will always truncate off the decimal or are you looking for something that will round in one direction? – Machavity Dec 06 '13 at 00:51

2 Answers2

13

Do you want to round it or get rid of the decimals?

To round it which would be 8933 would be:

echo utf8_encode(money_format('%.0n', $sales));

To get rid of the decimals, you could use floor (which rounds down):

echo utf8_encode(money_format('%.0n', floor($sales)));
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • Warning This function has been DEPRECATED as of PHP 7.4.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged. – user1077915 Jul 14 '21 at 17:05
  • `money_format()` should not be used because it was deprecated in PHP7.4 and removed in PHP8 https://www.php.net/manual/en/function.money-format.php – mickmackusa Apr 09 '22 at 06:13
  • @mickmackusa, yes. It's important to note that this answer is almost 9 years old and the question is also using money_format. – Devon Bessemer Apr 11 '22 at 14:06
  • 1. Questions are free to use whatever code they like, researchers are not supposed to copy-paste the asker's potentially broken/bad code. 2. If you would like more people to be helped by your post, it might be time for an edit. The first nudge from @user1077915 didn't work, so I thought I'd ping you again. – mickmackusa Apr 11 '22 at 20:33
  • @mickmackusa I thought about the change but I figured the new answer would work as well. Since the question specifically stated to "Use php money format", I decided not to change it since a different answer would not answer this specific question. People are free to edit answers as well. – Devon Bessemer Apr 12 '22 at 19:56
  • I will not be fundamentally changing your answer for you. If I was to do that, I may as well just write a whole new answer. When Stack Overflow rolls out their answer version tagging feature, I'll be sure to tag this one as deprecated/removed to inform researchers. – mickmackusa Apr 12 '22 at 20:49
1

As the money_format() has been deprecated in PHP 7.4 and removed in PHP 8.0, the suggested function for formatting currency is now NumberFormatter::CURRENCY.

Solving this problem for example would be done this way:

$sales = 8932.83;    
$sales = (int)$sales;      
$numFormat = new NumberFormatter("en_GB", NumberFormatter::CURRENCY);
$sales = $numFormat->formatCurrency($sales, "EUR");
$sales = str_replace('.00', '', $sales);
echo $sales;