And*_*ndi 1 php rounding floor
我有一个以mysql类型存储在mysql中的数字.从我读过的内容来看,我应该能够通过使用floor()来转换浮动,但尝试这个或其他任何东西都不起作用.希望有人能发现我做错了什么?
例..
数据库显示价格为25.00美元 - 在我的php页面中,我有以下代码(在将价格行转换为$ price后):
$price2 = floor($price);
echo $price;
echo '<br>';
echo $price2;
Run Code Online (Sandbox Code Playgroud)
我的结果是打印:
$25.00
0
Run Code Online (Sandbox Code Playgroud)
我也尝试用'round'代替'floor'.结果相同.
那是因为你正在使用它$25.00作为输入,并且$让PHP认为你正在尝试对字符串进行舍入 - PHP会将(非数字)字符串舍入为0.
floor =向下舍入.ceil =向上看.round =他们在文法学校教你的过程但是如果你$在字符串中有一个,那么这些都不会起作用.我建议你做点什么'$' . 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2446 次 |
| 最近记录: |