PHP 将值四舍五入到它自己的大小(十、百、千)
PHP round value to it's own size (ten, hunderd, thousand)
我想制作一个函数,将值四舍五入到它自己的大小。
示例:
5 => 10
51 => 60
100 => 100/200 // i don't mind which it will become
121 => 200
999 => 1000
1001 => 2000
我如何在 PHP 内完成此操作?我知道这可以使用 ceil/round 来完成,但我不知道怎么做。
您可以通过以下函数实现:
/**
* by Johannes Schuh, Pineapple Developer
*/
function roundIt($number) {
$amount = 0;
do {
$number = $number / 10;
$amount++;
} while($number >= 10);
$result = 0;
$result = $number - fmod($number, 1);
$result++;
$result = $result * pow(10, $amount);
return $result;
}
可能是你的意思
function Myround(array $numbers)
{
$new_numbers = [];
foreach ($numbers as $num) {
$rozriad = strlen($num)-1;
$new_numbers[] = round(ceil($num/pow(10, $rozriad))*pow(10, $rozriad), -$rozriad);
};
return $new_numbers;
};
$dozens = [5, 51, 100, 121, 999, 1001];
print_r(Myround($dozens));
我想制作一个函数,将值四舍五入到它自己的大小。
示例:
5 => 10
51 => 60
100 => 100/200 // i don't mind which it will become
121 => 200
999 => 1000
1001 => 2000
我如何在 PHP 内完成此操作?我知道这可以使用 ceil/round 来完成,但我不知道怎么做。
您可以通过以下函数实现:
/**
* by Johannes Schuh, Pineapple Developer
*/
function roundIt($number) {
$amount = 0;
do {
$number = $number / 10;
$amount++;
} while($number >= 10);
$result = 0;
$result = $number - fmod($number, 1);
$result++;
$result = $result * pow(10, $amount);
return $result;
}
可能是你的意思
function Myround(array $numbers)
{
$new_numbers = [];
foreach ($numbers as $num) {
$rozriad = strlen($num)-1;
$new_numbers[] = round(ceil($num/pow(10, $rozriad))*pow(10, $rozriad), -$rozriad);
};
return $new_numbers;
};
$dozens = [5, 51, 100, 121, 999, 1001];
print_r(Myround($dozens));