在PHP中使用精度舍入和舍入分数的函数,还有更好的方法吗? [英] Functions to Round up and Round down fractions with precision in PHP, any better way to do it?

查看:96
本文介绍了在PHP中使用精度舍入和舍入分数的函数,还有更好的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数round_up($ value)在php.net上找到这个代码,它适用于我的round_up部分。 ,$ precision = 0){
if(!empty($ value)){
$ sign =(0 <= $ value)? +1:-1;
$ amt = explode('。',$ value);
$ precision =(int)$ precision;
if(strlen($ amt [1])> $ precision){
$ next =(int)substr($ amt [1],$ precision);
$ amt [1] =(float)(('。'。substr($ amt [1],0,$ precision))* $ sign);
if(0!= $ next){
if(+1 == $ sign){
$ amt [1] = $ amt [1] +(float)(('。 '.str_repeat('0',$ precision - 1)。'1')* $ sign);



$ {
$ amt [1] =(float)(('。'。$ amt [1])* $ sign) ;
}
return $ amt [0] + $ amt [1];
}
else {echo'error';}
}

添加了一对减去..以得到这一个...

 函数round_down($ value,$ precision = 0){ 
if(!empty($ value)){
$ sign =(0 <= $ value)? +1:-1;
$ amt = explode('。',$ value);
$ precision =(int)$ precision;
if(strlen($ amt [1])> $ precision){
$ next =(int)substr($ amt [1],$ precision);
$ amt [1] =(float)(('。'。substr($ amt [1],0,$ precision))* $ sign);
if(0!= $ next){
if(-1 == $ sign){
$ amt [1] = $ amt [1] - (float)(('。 '.str_repeat('0',$ precision - 1)。'1')* $ sign);



$ {
$ amt [1] =(float)(('。'。$ amt [1])* $ sign) ;
}
return $ amt [0] + $ amt [1];
}
else {echo'error';}
}

有没有更好的方法来做到这一点? (我只需要它的正数小数)目前大多数情况下它没有太多的小故障..

  $ azd = 0.0130; 

$ azd1 = number_format(round_up($ azd,2),4);
$ azd2 = number_format(round_down($ azd,2),4);
$ azd3 = number_format(round_up($ azd,1),4);
$ azd4 = number_format(round_down($ azd,1),4);

echo'Round_up ='。$ azd1。'< br>'; // 0.0200
echo'Round_down ='。$ azd2。'< br>'; // 0.0100
echo'Round_up ='。$ azd3。'< br>'; // 0.1000
echo'Round_down ='。$ azd4。'< br>'; // 0.0000


解决方案

  function round_up($ value,$ precision = 0){
$ power = pow(10,$ precision);
return ceil($ value * $ power)/ $ power;
}
函数round_down($ value,$ precision = 0){
$ power = pow(10,$ precision);
返回楼层($ value * $ power)/ $ power;
}


I found this code on php.net which works for me for the round_up part..

function round_up($value, $precision = 0) { 
 if (!empty($value)) {
 $sign = (0 <= $value) ? +1 : -1; 
 $amt = explode('.', $value); 
 $precision = (int) $precision; 
 if (strlen($amt[1]) > $precision) { 
     $next = (int) substr($amt[1], $precision); 
     $amt[1] = (float) (('.'.substr($amt[1], 0, $precision)) * $sign); 
     if (0 != $next) { 
         if (+1 == $sign) { 
             $amt[1] = $amt[1] + (float) (('.'.str_repeat('0', $precision - 1).'1') * $sign); 
         } 
     } 
 } 
 else { 
     $amt[1] = (float) (('.'.$amt[1]) * $sign); 
 } 
 return $amt[0] + $amt[1]; 
 }
 else {echo 'error';} 
 }

Added a couple minus's.. to get this one...

function round_down($value, $precision = 0) { 
 if (!empty($value))  {
 $sign = (0 <= $value) ? +1 : -1; 
 $amt = explode('.', $value); 
 $precision = (int) $precision; 
 if (strlen($amt[1]) > $precision) { 
     $next = (int) substr($amt[1], $precision); 
     $amt[1] = (float) (('.'.substr($amt[1], 0, $precision)) * $sign); 
     if (0 != $next) { 
         if (-1 == $sign) { 
             $amt[1] = $amt[1] - (float) (('.'.str_repeat('0', $precision - 1).'1') * $sign); 
         } 
     } 
 } 
 else { 
     $amt[1] = (float) (('.'.$amt[1]) * $sign); 
 } 
 return $amt[0] + $amt[1]; 
 }
 else {echo 'error';}
 } 

Is there any better way to do it? (I only require it for positive decimals) Currently for the most part it works without much glitches..

$azd = 0.0130; 

$azd1 = number_format(round_up($azd,2),4);
$azd2 = number_format(round_down($azd,2),4);
$azd3 = number_format(round_up($azd,1),4);
$azd4 = number_format(round_down($azd,1),4);

echo 'Round_up = '.$azd1.'<br>'; // 0.0200
echo 'Round_down = '.$azd2.'<br>'; // 0.0100
echo 'Round_up = '.$azd3.'<br>';    // 0.1000
echo 'Round_down = '.$azd4.'<br>';  // 0.0000

解决方案

function round_up($value, $precision=0) {
    $power = pow(10,$precision);
    return ceil($value*$power)/$power;
}
function round_down($value, $precision=0) {
    $power = pow(10,$precision);
    return floor($value*$power)/$power;
}

这篇关于在PHP中使用精度舍入和舍入分数的函数,还有更好的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆