Php 定义跨平台货币格式函数(Linux和Windows)
我已经读到,money_格式在windows和一些Linux发行版(即BSD 4.11变体)上不可用。但我想在可用时使用普通函数编写跨平台库,在不可用时使用此函数,这样我的库就能够在每台基于PHP的web服务器上运行 是否有简单的解决方案来检查内置函数是否可用,如果不包括上面的解决方案?只有当系统具有strfmon功能时,才会定义money\u format()函数。例如,Windows没有,因此在Windows中未定义money_format() 因此,您可以使用以下php代码:Php 定义跨平台货币格式函数(Linux和Windows),php,formatting,numbers,number-formatting,money-format,Php,Formatting,Numbers,Number Formatting,Money Format,我已经读到,money_格式在windows和一些Linux发行版(即BSD 4.11变体)上不可用。但我想在可用时使用普通函数编写跨平台库,在不可用时使用此函数,这样我的库就能够在每台基于PHP的web服务器上运行 是否有简单的解决方案来检查内置函数是否可用,如果不包括上面的解决方案?只有当系统具有strfmon功能时,才会定义money\u format()函数。例如,Windows没有,因此在Windows中未定义money_format() 因此,您可以使用以下php代码: setloc
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']);
有了它,您可以编写实际上是可移植的代码,而不是依赖于操作系统特性。在PHP中使用money_format函数而不作为扩展是非常愚蠢的。我不明白您为什么要在一种编程语言的不同操作系统之间创建这样的不一致性在Windows机器上无法使用money\u format()
。以下是您针对印度货币格式的解决方案:
<?php
function inr_money_format($number){
$decimal = (string)($number - floor($number));
$money = floor($number);
$length = strlen($money);
$delimiter = '';
$money = strrev($money);
for($i=0;$i<$length;$i++){
if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$length){
$delimiter .=',';
}
$delimiter .=$money[$i];
}
$result = strrev($delimiter);
$decimal = preg_replace("/0\./i", ".", $decimal);
$decimal = substr($decimal, 0, 3);
if( $decimal != '0'){
$result = $result.$decimal;
}
return $result;
}
?>
您有现成的代码,那么问题是什么?我如何知道平台是否支持内置功能?如果函数存在
/可调用
可能与PHP5.5.9重复,我不想使用自定义函数,Windows 8.1 x64
该函数对$amount
不做任何处理,而不考虑区域设置。它在哪里解释负数?没有考虑负数格式的货币:)@blackmambo