Nasıl php yakın 10'a bir numara tamamlamaktadır olabilir?
I 23
, I 30
onu yuvarlamak için hangi kod kullanmak istiyorsunuz söylüyorlar?
round($number, -1);
Bu yakın 10'a $ numarayı yuvarlak olacaktır. Gerekirse de yuvarlama modunu değiştirmek için üçüncü bir değişken iletebilirsiniz.
Burada daha fazla bilgi: http://php.net/manual/en/function.round.php
Biz tur ile üzerinden "hile" olabilir
$rounded = round($roundee / 10) * 10;
Biz de birlikte kayan nokta bölünme geçiyor önleyebilirsiniz
function roundToTen($roundee)
{
$r = $roundee % 10;
return ($r <= 5) : $roundee - $r : $roundee + (10 - $r);
}
Düzenleme: Ben bilmiyordum (ve de sitede belgelenmiş değil) o round
şimdi "negatif" hassasiyet destekler, böylece daha kolay kullanabilirsiniz
$round = round($roundee, -1);
Yeniden düzenleme: her zaman yukarı yuvarlamak istiyorsanız, deneyebilirsiniz
function roundUpToTen($roundee)
{
$r = $roundee % 10;
if ($r == 0)
return $roundee;
return $roundee + 10 - $r;
}
Ben tarnfield @ istedi ve bu işlevi yazma sona erdi ne gibi esnek yuvarlama fonksiyonları arıyordu. Umarım biri yardımcı olabilir.
function round_down_to_integer($input_integer, $nearest_value = 0){
///Missing an input
if(!$input_integer || $input_integer < 1){return false;}
//Convert decimals to integers
$input_integer = round($input_integer,0);
$nearest_value = round($nearest_value,0);
//Can't round down any more
if($input_integer < $nearest_value){return false;}
//USED IN CALCULATION
if($nearest_value <= 10 && $nearest_value >= 0){$calc_upper_limit = 10;}
elseif($nearest_value <= 100 && $nearest_value > 10){$calc_upper_limit = 100;}
elseif($nearest_value <= 1000 && $nearest_value > 100){$calc_upper_limit = 1000;}
elseif($nearest_value <= 10000 && $nearest_value > 1000){$calc_upper_limit = 10000;}
//could go further to 100K, 1M...etc if you need it.
else{return false;}
//Intermediate Calculations
$subtract_by = $calc_upper_limit - $nearest_value;
$significant_digits_input = substr($input_integer, strlen($input_integer)-strlen($nearest_value),strlen($nearest_value));
//Final Calculations
if($significant_digits_input >= $nearest_value){$output = $input_integer - ($significant_digits_input - $nearest_value);}
else{$output = $input_integer - $significant_digits_input - $subtract_by;}
if($output > 0){return $output;}
else{return false;}
} //end function
///Test Function
for($i=10001; $i > 7999; $i--){
//Use any number 0-10000
echo number_format($i).' - '.number_format(round_down_to_integer($i, '0')).'<br>';
}
/*
Outputs
Input Output
10,001 - 10,000
10,000 - 10,000
9,999 - 9,990
9,998 - 9,990
9,997 - 9,990
9,996 - 9,990
9,995 - 9,990
9,994 - 9,990
9,993 - 9,990
9,992 - 9,990
9,991 - 9,990
9,990 - 9,990
9,989 - 9,980
9,988 - 9,980
9,987 - 9,980
9,986 - 9,980
9,985 - 9,980
9,984 - 9,980
9,983 - 9,980
9,982 - 9,980
9,981 - 9,980
9,980 - 9,980
9,979 - 9,970
9,978 - 9,970
9,977 - 9,970
9,976 - 9,970
9,975 - 9,970
9,974 - 9,970
9,973 - 9,970
9,972 - 9,970
9,971 - 9,970
9,970 - 9,970
*/