programing

어떻게 하면 10에 가까운 숫자를 반올림할 수 있을까요?

goodcopy 2022. 10. 27. 22:16
반응형

어떻게 하면 10에 가까운 숫자를 반올림할 수 있을까요?

어떻게 하면 php에서 가장 가까운 10으로 숫자를 반올림할 수 있을까요?

를 들어 '나에게 있다'고 하자.2330

floor()려갑니니다다

ceil()라갑니니다다

round()기본적으로는 가장 가까운 곳으로 이동합니다.

10으로 나누고 ceil을 한 다음 10을 곱하면 유효 자릿수를 줄일 수 있습니다.

$number = ceil($input / 10) * 10;

편집: 너무 오랫동안 이렇게 해왔어요.하지만 Tall Green은트리의 대답은 더 명확하다.

round($number, -1);

이렇게 하면 $10을 반올림할 수 있습니다.반올림 모드를 변경하는 데 필요한 경우 세 번째 변수를 전달할 수도 있습니다.

자세한 내용은 이쪽:http://php.net/manual/en/function.round.php

실제로 가장 가까운 변수로 반올림할 수 있는 함수를 찾고 있었는데 검색에서 이 페이지가 계속 뜨더군요.그래서 결국 내가 직접 그 기능을 쓰게 되었을 때, 다른 사람들이 찾을 수 있도록 여기에 올려야겠다고 생각했다.

함수는 가장 가까운 변수로 반올림합니다.

function roundToTheNearestAnything($value, $roundTo)
{
    $mod = $value%$roundTo;
    return $value+($mod<($roundTo/2)?-$mod:$roundTo-$mod);
}

다음 코드:

echo roundToTheNearestAnything(1234, 10).'<br>';
echo roundToTheNearestAnything(1234, 5).'<br>';
echo roundToTheNearestAnything(1234, 15).'<br>';
echo roundToTheNearestAnything(1234, 167).'<br>';

유언 출력:

1230
1235
1230
1169

이 질문에는 많은 답변자가 있으며, 아마도 모두 여러분이 원하는 답을 줄 것입니다.그러나 @TallGreenTree에서 언급했듯이 이를 위한 기능이 있습니다.

그러나 @Tall Green Tree의 답변의 문제는 반올림되지 않고 가장 가까운 10으로 반올림된다는 것입니다.를 해결하려면 , 「」를 합니다.+5반올림 할 수 있도록 번호를 입력해 주세요. 반올림을 -5.

코드:

round($num + 5, -1);

하면 안 요.round mode반올림만 하면 정수가 아니라 부분만 반올림하기 때문입니다.

「」로 .100 , 을 사용해야 .+50.

10으로 나눈 다음 ceil을 사용한 다음 10으로 멀티를 사용합니다.

http://php.net/manual/en/function.ceil.php

우리는 라운드를 통해 다음과 같이 "사기"할 수 있다

$rounded = round($roundee / 10) * 10;

또한 부동소수점 분할을 피할 수 있습니다.

function roundToTen($roundee)
{
  $r = $roundee % 10;
  return ($r <= 5) : $roundee - $r : $roundee + (10 - $r);
}

편집: 몰랐습니다(또한 사이트에는 제대로 문서화되어 있지 않습니다).round ' 정밀도를하므로 '네거티브'를 더 쉽게 할 수 .

$round = round($roundee, -1);

다시 편집:항상 반올림하고 싶다면

function roundUpToTen($roundee)
{
  $r = $roundee % 10;
  if ($r == 0)
    return $roundee;
  return $roundee + 10 - $r;    
}

해라

round(23, -1);

$value = 23;
$rounded_value = $value - ($value % 10 - 10);
//$rounded_value is now 30

가장 큰 자리수의 다음 자리까지 반올림하고 싶어서(그 이름이 있나요?), 아래 함수(php)를 만들었습니다.

//Get the max value to use in a graph scale axis, 
//given the max value in the graph
function getMaxScale($maxVal) {
    $maxInt = ceil($maxVal);
    $numDigits = strlen((string)$maxInt)-1; //this makes 2150->3000 instead of 10000
    $dividend = pow(10,$numDigits);
    $maxScale= ceil($maxInt/ $dividend) * $dividend;
    return $maxScale;
}

이것을 시험해 보세요.

ceil($roundee / 10) * 10;

가장 가까운 10까지, 다음과 같아야 합니다.

$number = ceil($input * 0.1)/0.1 ;

저의 첫 번째 충동은 "php math"를 검색해보고 싶었고, "round()"라고 불리는 핵심 수학 라이브러리 함수가 여러분이 원하는 함수라는 것을 발견했습니다.

php, java, python 등을 사용하지 않고 raw SQL을 사용하고 싶은 사용자용. SET SQL_SAFE_UPDATES = 0; UPDATE db.table SET value=ceil(value/10)*10 where value not like '%0';

Hey i modify Kenny answer and custom it not always round function now it can be ceil and floor function

function roundToTheNearestAnything($value, $roundTo,$type='round')
    {
        $mod = $value%$roundTo;
        if($type=='round'){
            return $value+($mod<($roundTo/2)?-$mod:$roundTo-$mod);
        }elseif($type=='floor'){
            return $value+($mod<($roundTo/2)?-$mod:-$mod);
        }elseif($type=='ceil'){
            return $value+($mod<($roundTo/2)?$roundTo-$mod:$roundTo-$mod);
        }

    }

echo roundToTheNearestAnything(1872,25,'floor'); // 1850<br>
echo roundToTheNearestAnything(1872,25,'ceil'); // 1875<br>
echo roundToTheNearestAnything(1872,25,'round'); // 1875

이는 PHP 'fmod' 함수를 사용하여 쉽게 수행할 수 있습니다.아래 코드는 10에 고유하지만 원하는 숫자로 변경할 수 있습니다.

$num=97;
$r=fmod($num,10);
$r=10-$r;
$r=$num+$r;
return $r;

출력: 100

이렇게 해보세요... 반올림할 숫자를 입력하면 10분의 1로 반올림됩니다. 도움이 되길 바랍니다...

반올림수, 1);

언급URL : https://stackoverflow.com/questions/1619265/how-to-round-up-a-number-to-nearest-10

반응형