我有2个小数位的问题,我希望它是35.99美元而不是3599美元。
当我为了测试目的而不断刷新页面时,会出现此问题。有时价格显示没有小数位,但在我继续测试我的页面后恢复正常。
有谁知道为什么会这样?
下面是我的PHP代码:
// format for currency values
function cartweaverMoney($theNum) {
$cwLocaleInfo = localeconv();
$cwLocaleInfo["currency_symbol"] = 'USD ';
$retStr = number_format($theNum,
2,
$cwLocaleInfo["mon_decimal_point"],
$cwLocaleInfo["mon_thousands_sep"]);
if ($cwLocaleInfo["p_cs_precedes"]) {
if ($cwLocaleInfo["p_sep_by_space"]) {
$retStr = " ".$retStr;
}
$retStr = $cwLocaleInfo["currency_symbol"].$retStr;
} else {
if ($cwLocaleInfo["p_sep_by_space"]) {
$retStr .= " ";
}
$retStr .= $cwLocaleInfo["currency_symbol"];
}
return $retStr;
}
答案 0 :(得分:0)
在localeconv()之前,您必须使用'setlocale':
setlocale(LC_ALL, "en_US");
$locale_info = localeconv();
http://php.net/manual/it/function.localeconv.php
或者,也许,检查此代码: http://php.net/manual/it/function.number-format.php
<?php
$var = number_format(2234,2);
$var = number_format($var,2);
echo $var;
# Expected Output: 2,234.00
# Actual Output: 2.00
?>
//To fix, remove the thousands separator by setting the decimal point and thousands separator parameters like so:
<?php
$var = number_format(2234,2,'.','');
$var = number_format($var,2,'.','');
echo $var;
# Expected Output: 2234.00
# Actual Output: 2234.00
?>
享受您的代码!
编辑: 我已经在Windows系统中打印了localconv()数组返回,它是无效的。 setloca
le(LC_ALL, "en_US");
$cwLocaleInfo = localeconv();
function print_locale_conv($locale_info) {
echo "<pre>\n";
echo "--------------------------------------------\n";
echo " Informazioni sulla configurazione monetaria locale: \n";
echo "--------------------------------------------\n\n";
echo "int_curr_symbol: {$locale_info["int_curr_symbol"]}\n";
echo "currency_symbol: {$locale_info["currency_symbol"]}\n";
echo "mon_decimal_point: {$locale_info["mon_decimal_point"]}\n";
echo "mon_thousands_sep: {$locale_info["mon_thousands_sep"]}\n";
echo "positive_sign: {$locale_info["positive_sign"]}\n";
echo "negative_sign: {$locale_info["negative_sign"]}\n";
echo "int_frac_digits: {$locale_info["int_frac_digits"]}\n";
echo "frac_digits: {$locale_info["frac_digits"]}\n";
echo "p_cs_precedes: {$locale_info["p_cs_precedes"]}\n";
echo "p_sep_by_space: {$locale_info["p_sep_by_space"]}\n";
echo "n_cs_precedes: {$locale_info["n_cs_precedes"]}\n";
echo "n_sep_by_space: {$locale_info["n_sep_by_space"]}\n";
echo "p_sign_posn: {$locale_info["p_sign_posn"]}\n";
echo "n_sign_posn: {$locale_info["n_sign_posn"]}\n";
echo "</pre>\n";
}
?>
所以我读过这个: http://php.net/manual/it/function.setlocale.php
我已经更改了localeconv()的参数:
$lang = "us_US";
$language = 'english';
$codeset = "UTF8"; // warning ! not UTF-8 with dash '-'
date_default_timezone_set('Europe/Rome');
setlocale(LC_ALL, array($lang.'.'.$codeset,$lang.'@euro',$lang,$language));
$cwLocaleInfo = localeconv();
print_locale_conv($cwLocaleInfo);
惠特这些作品!
对于* nix系统,请尝试使用此:
$lang = "us_US";
$language = 'english';
$codeset = "UTF8"; // warning ! not UTF-8 with dash '-'
putenv('LANG='.$lang.'.'.$codeset);
putenv('LANGUAGE='.$lang.'.'.$codeset);