Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use php money format and remove the numbers after the decimal place for GBP

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, '', ',');
like image 716
Ben Paton Avatar asked Dec 06 '13 00:12

Ben Paton


People also ask

How can I get one decimal place in PHP?

$one_decimal_place = number_format(2.10, 1);

What is the decimal place for money?

United States (U.S.) currency is formatted with a decimal point (.) as a separator between the dollars and cents. Some countries use a comma (,) instead of a decimal to indicate that separation.

Does PHP Number_format round?

The PHP number_format() function is used for formatting numbers with decimal places and thousands separators, but it also rounds numbers if there are more decimal places in the original number than required. As with round() it will round up on 5 and down on < 5.


1 Answers

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)));
like image 88
Devon Avatar answered Sep 20 '22 17:09

Devon