那是因为您将$25.00
其用作输入并且$
使 PHP 认为您正在尝试对字符串进行四舍五入——PHP 会将(非数字)字符串四舍五入为 0。
floor
= 向下舍入。
ceil
=四舍五入。
round
= 他们在文法学校教给你的过程相同
$
但是如果字符串中有 a ,这些都不起作用。我建议你做类似的事情'$' . round( str_replace( '$', '', $price ) * 100 ) / 100
。(乘法和除法使它四舍五入到最接近的一美分(而不是美元),str_replace
使它处理一个数值,然后在前面加上 a $
。如果你真的很花哨,然后按照下面)
$dollar = '$' . round( str_replace( '$', '', $price ) * 100 ) / 100;
// the following makes sure that there are two places to the right of the decimal
$pieces = explode( '.', $dollar );
if( isset($pieces[1]) && strlen( $pieces[1] ) == 1 )
{
$pieces[1].='0';
$dollar = implode('.', $pieces);
}
// if you like, you can also make it so that if !pieces[1] add the pennies in