最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there a PHP function equivalent to JavaScript function toLocaleString() - Stack Overflow

programmeradmin1浏览0评论

I was looking for a pre-built number formatting function and came across this JS function toLocaleString(). This function does exactly what I want it to do.

For example, I have a number which I want to be formatted, let's say 1234567890.123. I want this number to be formatted into 1,23,45,67,890.123 & 1,234,567,890.123.

With the JS function I did it like this and got the desired output

var number = 1234567890.123;
number.toLocaleString('hi-IN'); //1,23,45,67,890.123
number.toLocaleString('en'); //1,234,567,890.123

However, I wanted to know if there is a built in method to do this with PHP.

NOTE: money_format() is not what I'm looking for and I just want to know if there exists such a function in PHP too. If not, no problem, I'll have to write a custom function.

I was looking for a pre-built number formatting function and came across this JS function toLocaleString(). This function does exactly what I want it to do.

For example, I have a number which I want to be formatted, let's say 1234567890.123. I want this number to be formatted into 1,23,45,67,890.123 & 1,234,567,890.123.

With the JS function I did it like this and got the desired output

var number = 1234567890.123;
number.toLocaleString('hi-IN'); //1,23,45,67,890.123
number.toLocaleString('en'); //1,234,567,890.123

However, I wanted to know if there is a built in method to do this with PHP.

NOTE: money_format() is not what I'm looking for and I just want to know if there exists such a function in PHP too. If not, no problem, I'll have to write a custom function.

Share Improve this question asked Feb 25, 2018 at 7:36 Rohit KapaliRohit Kapali 2163 silver badges10 bronze badges 1
  • 1 Check the Internationalization Functions, probably a class for whatever you might need, like NumberFormatter – Patrick Evans Commented Feb 25, 2018 at 7:44
Add a ment  | 

3 Answers 3

Reset to default 3

PHP does allow for Number Formatting, but does not have a function that can do exactly as Javascript's toLocaleString().

The best equivelant is provided here: How to display Currency in Indian Numbering Format in PHP

PHP's NumberFormatter can be used to format decimals. The JavaScript implementation sets the locale value on the client side of the browser and then outputs the results.

PHP has a similar method for setting the locale value, but is performed on the server. This could result in unintended consequences.

"Warning: The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS, HHVM or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale()." Source: http://php/manual/en/function.setlocale.php

use

$x = 45604586;
number_format($x);

output: 45,604,586

Following the link about The NumberFormatter class in this answer I was able to make the exact things I do with toLocaleString() in JavaScript:

$locale = 'en';
$currency = 'EUR';
$price = 45604586;
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$formattedPrice = $fmt->formatCurrency($price, $currency);
echo $formattedPrice;

It prints:

€45,604,586.00
发布评论

评论列表(0)

  1. 暂无评论