문제

우리는 어떻게 둥글게 숫자를 가장 가까이에서 10php?

23, 무엇 코드를 사용하여 라운드 그것을 30?

도움이 되었습니까?

해결책

floor() 이 아래로 이동합니다.

ceil() 올라갈 것입니다.

round() 갈 것입니다 가까운 기본적으로 합니다.

나눌 10 을 수행합니 ceil,다음을 곱하여 10 를 줄이는 중요한 자리입니다.

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

편집:나는 그것을 이런 식으로 일을 하는 것은니다.하지만 TallGreenTree 의 대답은 깨끗합니다.

다른 팁

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

많은 anwers 이 질문에 아마도 모든 것이 당신에게 답을 찾고 있습니다.하지만@TallGreenTree 언급하는 기능이 있습니다.

그러나 문제의 답을@TallGreenTree 지 않는다 라운드까지,그것은 라운드에 가장 가까운 10.이를 해결하기 위해 추가 +5 귀하의 번호하기 위해서다.하려는 경우 라운드,아래 마 -5.

그래서 코드:

round($num + 5, -1);

사용할 수 없습니다 round mode 을 위한 반올림하기 때문에 그만 라운드까지 분수와 전체적인 숫자입니다.

하려는 경우 라운드에 가장 가까운 100, 에,당신은 당신이 사용 +50.

div10 다음 사용하 ceil 다음 mult10

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

round(23, -1);

우리는"치"를 통해 라운드

$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;    
}
$value = 23;
$rounded_value = $value - ($value % 10 - 10);
//$rounded_value is now 30

이것을 보십시오:

ceil($roundee / 10) * 10;

다만 라운드 아래로 가장 가까운 10,그리고 다음에 추가 10.

round($num, -1) + 10

내 동글에 대한"php 수학은"나는 것이 있음을 발견했습의 핵심 수학 라이브러리 함수 호출 라운드"()"는 가능성이 높은 무엇을 원합니다.

원하는 사람들을 위해 그것을 할로 원 SQL 사용하지 않고,php,java,python 등입니다.SET SQL_SAFE_UPDATES = 0; UPDATE db.table SET value=ceil(value/10)*10 where value not like '%0';

내가 원하는 라운드까지 다음 번에 가장 큰 숫자 장소(이 있는 이름?), 그래서 나는 다음과 같은 함수는(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;
}
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 를 사용하여'세상에서 가장 재미'기능이 있습니다.아래 코드는 특정 10 지만 당신은 그것을 변경할 수 있습을 얼마든지 가능합니다.

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

출력:100

이 시도하고......에 전달될 수를 반올림하고 그것이 떨어져 가장 가까운 십.도움이 되기를 바랍니다....

라운드($num,1);

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top