Setlocale to fr-FR in PHP and number formatting

I am trying to create a french version of one of my sites. I set setlocale(LC_ALL, 'fr_FR'); at the top of my page and using strftime , I correctly display dates in french style.

However, I am having some problems with the number. Part of the page uses the data that I get from the web service. I assign a return value to var as follows:

 $overallRating = $returnArray['Overall']; 

Then I use the following code later to format it to 1 decimal point.

 number_format($overallRating,1) 

In the English version, the value of totalRating can be 7.5 and returns a value, for example 7.5 (the reason for running it through number_format is if it returns 7 , I want it to display 7.0 )

However, in the French version, if I print the original value of $ totalRating, I get 7,5 , which looks like it translated it into French. That would be nice, but if I run it through number_format , I get an error:

 Notice: A non well formed numeric value encountered in /sites/index.php on line 250 

Line 250 is the line number_format

I assume that translating to 7,5 will ruin it, but I'm not sure how to solve it ...

Using PHP 5.3 in case something new helps me

+4
source share
3 answers

So in the end I found the answer after many studies.

PHP really works with standard US decimal separators and can't handle French, seperator.

The answer, although not perfect, means reset the numerical values ​​for using US, allowing you to format dates, etc. in French. Placement of this line:

  setlocale(LC_NUMERIC, 'C'); 

Under

  setlocale(LC_ALL, 'fr_FR'); 

did the trick

+6
source

Is this what you ask?

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

Later Edit:

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

I wanted to take a line of type β€œ54.33” edited from France and save it back to db as en_US number, because, as already mentioned, PHP really loves only numbers in en_US format. I have not seen any built-in PHP conversion functions that were built to perform this task. Removes the thousands separator and replaces the decimal separator with "." and it is designed to work with any setting.

 //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 will build a similar thing for JavaScript. This library was found for display: http://software.dzhuvinov.com/jsworld-currency-formatting.html

+1
source

Source: https://habr.com/ru/post/1310874/


All Articles