admin管理员组文章数量:1310969
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.
Share Improve this question asked Feb 25, 2018 at 7:36 Rohit KapaliRohit Kapali 2163 silver badges10 bronze badges 1NOTE: 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.
- 1 Check the Internationalization Functions, probably a class for whatever you might need, like NumberFormatter – Patrick Evans Commented Feb 25, 2018 at 7:44
3 Answers
Reset to default 3PHP 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
本文标签: Is there a PHP function equivalent to JavaScript function toLocaleString()Stack Overflow
版权声明:本文标题:Is there a PHP function equivalent to JavaScript function toLocaleString() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741834851a2400151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论