当前位置 : 主页 > 网络编程 > PHP >

对大于一万的数字保留前两位其余补0,并显示中文单位

来源:互联网 收集:自由互联 发布时间:2021-06-28
对大于一万的数字保留前两位其余补0 // 补0function rounding($num){ $count = strlen($num); $firstNum = intval(substr($num, 0, 1)); $secondNum = intval(substr($num, 1, 1)); $thirdNum = intval(substr($num, 2, 1)); $restNum = $co
对大于一万的数字保留前两位其余补0
// 补0
function rounding($num)
{
    $count = strlen($num);
    $firstNum = intval(substr($num, 0, 1));
    $secondNum = intval(substr($num, 1, 1));
    $thirdNum = intval(substr($num, 2, 1));
    $restNum = $count-1;
    if ($count >= 5) {
        if ($thirdNum >= 5) {
            if ($secondNum == 9) {
                $firstNum += 1;
                $secondNum = 0;
                $thirdNum = 0;
                $num = $firstNum*pow(10, $restNum);
            } else {
                $secondNum += 1;
                $thirdNum = 0;
                $num = ($firstNum*10+$secondNum)*pow(10, $count-2); // 78600 79000
            }
        }
    }
    return $num;
}

// 显示中文单位
function downnumConversion($downnum)
{
    $downnum = rounding($downnum);
    $count = strlen($downnum);
    if ($count >= 5 && $count < 9) {
        $previousNum = substr($downnum, 0, $count-4);
        $downnum = $previousNum.'万';
    } elseif ($count >= 9) {
        $previousNum = substr($downnum, 0, $count-8);
        $nextNum = substr($downnum, 1, $count-8);
        if ($nextNum) {
            $downnum = $previousNum.'.'.$nextNum.'亿';
        } else {
            $downnum = $previousNum.'亿';
        }
    }
    return $downnum;
}
网友评论