Possible Duplicate:
PHP: show a number to 2 decimal places
How can I format an input number to be 0.00 if it has not any value? I tried (double) but it prints 0 only.
Possible Duplicate:
PHP: show a number to 2 decimal places
How can I format an input number to be 0.00 if it has not any value? I tried (double) but it prints 0 only.
干得好 :)
echo number_format($var,2);
如果您希望它打印特定的编号。小数点,使用number_format
.
$float_var = number_format($var, 2);
$var = number_format($number, 2, '.', '');
这将强制小数点后 2 点,将小数点设置为句点。您也可以放弃最后两个,因为它默认为它;
注意:第三个值是您的小数分隔符,第四个值是千分位分隔符。
$var = number_format($number, 2);
如果未设置值,也许您应该先检查它
if(!isset($variableName))
{
// then set
$variableName = "0.00"; // => string
//or like this
$variableName = number_format(0,2); // => this result is also string
}
echo "value: ",$variableName;
结果
0.00
对于直接输出:
printf('%0.2f',$var);
输出到变量:
$outVar = sprintf('%0.2f',$var);
此语句将 $var 类型转换为浮点数并使用 2 个十进制符号打印
如果您尝试格式化值,我建议您使用 meioMask 插件。因此,您将您的字段定义为数字,并且即使您将值设置为“0”,插件也可以解决问题