定义跨平台的money_format函数(Linux和Windows) [英] Defining cross-platform money_format function (Linux and Windows)
问题描述
有没有简单的解决方案来检查是否内置函数是可用的,如果不包含从上面的解决方案?
功能money_format()只是定义如果系统具有strfmon功能。例如,Windows没有,所以money_format()在Windows中是未定义的。
所以你可以使用这个php代码:
setlocale(LC_ALL,''); //区域设置在每个系统上都会有所不同。
$ amount = 1000000.97;
$ locale = localeconv();
echo $ locale ['currency_symbol'],number_format($ amount,2,$ locale ['decimal_point'],$ locale ['thousands_sep']);
使用此功能,您可以编写实际可移植的代码,而不依赖于操作系统功能。在PHP中使用money_format函数没有扩展是非常愚蠢的。我不明白你为什么要用编程语言在不同的操作系统之间创建这样的不一致之处。
I have read that money_format is not available on windows, and on some Linux distributions (i.e. BSD 4.11 variants). But I want to write cross-platform library using normal function, when available and using this workaround when not, so my library will be able to run on every PHP-based web server.
Is there any simple solution to check whether built-in function is available and if not to include the solution from above?
The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.
So you can use this php code:
setlocale(LC_ALL, ''); // Locale will be different on each system.
$amount = 1000000.97;
$locale = localeconv();
echo $locale['currency_symbol'], number_format($amount, 2, $locale['decimal_point'], $locale['thousands_sep']);
With this you can write code that is actually portable instead of relying on operating system features. Having the money_format function available in PHP without it being an extension is pretty stupid. I don’t see why you would want to create inconsistencies like this between different operating systems in a programming language
这篇关于定义跨平台的money_format函数(Linux和Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!