Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP money format with spaces

I am wondering if there is a way to separate thousands with space character.

For example:

$number = 21234.56;
setlocale(LC_ALL, 'pl_PL.utf8');
echo money_format('%i', $number);

is giving me:

21.234,56 PLN

And I want:

21 234,56 PLN

I don't want to make str_replace. I am sure there is a better way.

like image 263
Cleankod Avatar asked Mar 16 '11 08:03

Cleankod


People also ask

What is number format PHP?

The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure. Syntax: string number_format ( $number, $decimals, $decimalpoint, $sep )


1 Answers

You can use number-format. For example :

echo number_format($number, 2, ',', ' ');

But you will have to put the unit yourself.

Otherwise, this comment of the money_format documentation page gives a version of the function with some code that you can modify to change the thousand separator.

You can also do something like this :

$locale = localeconv();
$text = money_format('%i', $number);
$text = str_replace($locale['mon_thousands_sep'], ' ', $text);
like image 109
krtek Avatar answered Oct 02 '22 22:10

krtek