setlocale to fr-FR in PHP and number formatting

风格不统一 提交于 2019-12-01 07:41:27

So eventually found the answer after a lot of research.

PHP only really works with standard US decimal seperators and can't cope with the french , seperator.

The answer, although not perfect is to reset numeric values to use US while allowing dates etc to be formatted in French. Placing this line:

 setlocale(LC_NUMERIC, 'C');

Under

 setlocale(LC_ALL, 'fr_FR'); 

has done the trick

is this what you are asking ?

<?php
    $overallRating = number_format($number, 1, '.', '');
?>

Later Edit:

<?php
setlocale(LC_ALL, 'fr_FR');
$number = '7';
echo $overallRating = number_format($number, 1, '.', '');
?>

I wanted to take a French-edited string like "54,33" and save it back to the db as en_US number because, as has been pointed out, PHP really only likes numbers in the en_US format. I didn't see any built-in PHP conversion functions so built this to do the job. Removes the thousands separator and replaces the decimal separator with "." and it's designed to work with any setup.

//Currency Conversion Back to US for DB Storage or PHP number formatting
function number_convert_us($num) {
    $locale_settings = localeconv();
    $num = str_replace($locale_settings['mon_thousands_sep'], "", trim($num));
    $num = str_replace($locale_settings['mon_decimal_point'], ".", $num);
    return number_format((double)$num, 2, '.', '');
}

I'll build a similar thing for JavaScript. Found this library for display: http://software.dzhuvinov.com/jsworld-currency-formatting.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!