これは$25.00
、入力として使用していて、$
PHPが文字列を丸めようとしていると見なすためです。PHPは(数値ではない)文字列を0に丸めます。
floor
=切り捨て。
ceil
=切り上げ。
round
=彼らがグラマースクールで教えたのと同じプロセス
ただし、文字列にが含まれている場合、これらはいずれも機能しません$
。のようなことをすることをお勧めします'$' . round( str_replace( '$', '', $price ) * 100 ) / 100
。(乗算と除算により、(ドルではなく)最も近いペニーに丸められstr_replace
、数値を処理するようになり、その後、先頭に追加$
されます。本当に空想している場合は、以下に従ってください。 )。
$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