价格中缺少 2 位小数(PHP)
Posted
技术标签:
【中文标题】价格中缺少 2 位小数(PHP)【英文标题】:2 decimal places missing in price(PHP) 【发布时间】:2014-10-04 04:27:33 【问题描述】:我有 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;
【问题讨论】:
您一定要检查这两个变量 $cwLocaleInfo["mon_decimal_point"]、$cwLocaleInfo["mon_thousands_sep"] 中的内容,以确保您有有效的分隔符等 我想你指的是这段代码?我不熟悉 php,但我发现此代码与带有标题的相同 php 页面//用于 js 货币格式的 LOCALE SETTINGS $_ENV["application.cw"]["currencyPrecedes"] = false; $_ENV["application.cw"]["currencySymbol"] = ""; $_ENV["application.cw"]["currencyDecimal"] = "."; $_ENV["application.cw"]["currencyGroup"] = ","; $_ENV["application.cw"]["currencySpace"] = ""; 【参考方案1】:在 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() 数组返回,它是无效的。 设置位置
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);
【讨论】:
我试图添加这个 >> _italic setlocale(LC_ALL, "en_US") > $cwLocaleInfo = localeconv(); 感谢Magicianred的及时回复,我稍后试试。 :)以上是关于价格中缺少 2 位小数(PHP)的主要内容,如果未能解决你的问题,请参考以下文章