四舍五入到一位小数

Rounding down to one decimal

我正在使用一个函数来计算 1000 到 1k 和 1000000 到 1m 但我想给它加一位小数,我已经做到了这一点但是它四舍五入而不是向下 所以如果我输入 1565 我想得到 1.5 只有当我通过 1600 我想得到 1.6(强制向下舍入)我使用这个函数:

public function convert($Input){
    if($Input<1000){
        $AmountCode = "";
        $Amount = $Input;
    }
    else if($Input>=1000000){
        $AmountCode = "M";
        $Amount = round(floatval($Input / 1000000), 1, PHP_ROUND_HALF_DOWN);
    }
    else if($Input>=1000){
        $AmountCode = "K";
        $Amount = round($Input / 1000, 1, PHP_ROUND_HALF_DOWN);

    }

    $Array = array(
        'Amount' => $Amount,
        'Code' => $AmountCode
    );

    return $Array;

}

场景一: 现在如果我输入 1565 我得到 1.6 会发生什么,如果我输入 1545 我得到 1.5 有没有人知道如何强制向下取整?

场景 2:我输入的数字 10665 将输出为 10.7k(四舍五入),但我想显示为 10.6k(四舍五入),出于某种原因我不知道该怎么做那

为了在 PHP 中向下舍入,他们有一个名为 floor() 的函数。不幸的是,这个函数只做 return 一个 int。但是,您可以先乘法然后除法使其向下舍入。看到这个 post:PHP How do I round down to two decimal places?.

这意味着您的代码应该是这样的:

else if($Input>=1000000){
    $AmountCode = "M";
    $Amount = floor(floatval($Input / 100000))/10;
}
else if($Input>=1000){
    $AmountCode = "K";
    $Amount = floor(floatval($Input / 100))/10;

}