Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP money_format not working

Tags:

php

I'm using Laravel 4 on TurnkeyLinux and trying to get money_format to display currency in a localized manner.

money_format('%.2n', 1222002.09) returns 1222002.09.

In app/strart/global.php I have App::setLocale(Session::get('locale', 'en'));, this alters the language using Laravel's language files but has no effect on currency.

I've discovered that localeconv(); outputs a nearly empty array (only decimal point is set) and using setLocale(LC_ALL, 'en_GB', 'en_GB'); has no effect.

I'm on PHP 5.4.4 and Debian 3.2.57.

like image 763
SteB Avatar asked Aug 20 '14 07:08

SteB


People also ask

What is money format?

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.


1 Answers

Got it! Thanks to Dmitry Bezik for pointing me in the right direction.

locale -a returned:

  • C
  • C.UTF-8
  • POSIX

The lack of GB (or US) locales prompted me to do another search that found me this page.

Basically I hadn't got any locales installed, so I followed the instructions and installed the GB locale by doing the following:

  1. Opened /etc/locale.gen using WinSCP and scrolled down the list of commented-out locales until I found en_GB.UTF-8 UTF-8 and removed the leading "#", (if your file is empty, just adding an entry should work).
  2. Ran /usr/sbin/locale-gen as root, this made the system "Generate locales" (which only took a second).
  3. Added setLocale(LC_ALL, 'en_GB.utf8', 'en_GB'); to my blade template and refreshed.

Voilà! I now get £1,222,002.09.
I then moved my setLocale code into my app/start/global.php just after I set the language so it run on every request.

like image 92
SteB Avatar answered Oct 18 '22 04:10

SteB