定义跨平台的money_format函数(Linux和Windows) [英] Defining cross-platform money_format function (Linux and Windows)

查看:270
本文介绍了定义跨平台的money_format函数(Linux和Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过,money_format在Windows和某些Linux发行版(即BSD 4.11变体)上不可用。但是我想用正常的函数编写跨平台的库,当可用的时候使用这个解决方法,所以我的库将能够运行在每个基于PHP的web服务器上。



有没有简单的解决方案来检查是否内置函数是可用的,如果不包含从上面的解决方案?

解决方案

功能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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆