Round up to nearest multiple of five in PHP
This can be accomplished in a number of ways, depending on your preferred rounding convention:
1. Round to the next multiple of 5, exclude the current number
Behaviour: 50 outputs 55, 52 outputs 55
function roundUpToAny($n,$x=5) {
return round(($n+$x/2)/$x)*$x;
}
2. Round to the nearest multiple of 5, include the current number
Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 50
function roundUpToAny($n,$x=5) {
return (round($n)%$x === 0) ? round($n) : round(($n+$x/2)/$x)*$x;
}
3. Round up to an integer, then to the nearest multiple of 5
Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 55
function roundUpToAny($n,$x=5) {
return (ceil($n)%$x === 0) ? ceil($n) : round(($n+$x/2)/$x)*$x;
}
Round integer to nearest multiple of 5 in PHP
Use round()
instead of ceil()
.
5 * round($n / 5);
ceil()
rounds a floating point number up to its next integer in sequence. round()
will round to the nearest integer using standard rounding rules.
round to nearest multiple of 5 in a range in php
Let say $x
has the number:
$x = 39;
If you want the closest multiple of 5 (39 --> 40):
$x = round($x / 5) * 5;
If you want to round up (36 --> 40):
$x = ceil($x / 5) * 5;
If you want to round down (39 --> 35):
$x = floor($x / 5) * 5;
After defining $x
, you can use the following to make sure its in the 25-45 range:
$x = ($x > 45) ? 45 : ($x < 25) ? 25 : $x;
Round to nearest 5 or 9 in PHP
This is a bit, non mathy but should work:
function round9($n) {
$n = ceil($n);
$r = substr($n,-1) > 5 ? 9 : 5;
$n = substr($n,0,-1) . $r;
return $n;
}
- first round up to nearest integer
- get last digit
- replace last digit with 9 if it's greater than 5. otherwise replace with a 5.
UPDATE: I thought this was an interesting problem and now that we've got three correct answers which solve it from very different angles I thought I'd do a bit of performance analysis for my own curiosity. neuro_sys's answer wins by far! Here are the results in seconds to complete 10 million iterations:
time to build array: 1.7170281410217
round9: 10.753921985626
my_round: 1.6339750289917
rounder: 16.578145980835
Test was run on an 8GB, 4 Core Linode VPS running Ubuntu 16.04 / PHP 7.0.14
How can I round down to the nearest multiple of 3 in PHP?
Is this what you looking for.
/**
* Round an integer down to the nearest multiple of the given multiple
* @param integer $number
* @param integer $multiple
* @return integer
*/
function round_down_to_multiple_of( int $number, int $multiple = 3 ): int
{
return (int)($multiple * floor( $number/$multiple ));
}
# TESTS
$numbers = [ 10, 9, 8, 1, 0 ];
foreach( $numbers as $number ){
printf( '%d became %d'.PHP_EOL, $number, round_down_to_multiple_of( $number, 3 ) );
}
After running the above test I get the following results:
10 became 9
9 became 9
8 became 6
1 became 0
0 became 0
PHP Round to previous multiple of 5 decimal
an easy solution, floor the double number and divide by two... 2 cents ;)
<?php
function roundToPreviousMultipleWithDecimal(float $number){
return $specificRound = floor($number * 2) / 2;
}
$a = 8.4;
$b = 8.6;
$c = 8.9;
$d = 9.0;
echo roundToPreviousMultipleWithDecimal($a) ."<br />";
echo roundToPreviousMultipleWithDecimal($b) ."<br />";
echo roundToPreviousMultipleWithDecimal($c) ."<br />";
echo roundToPreviousMultipleWithDecimal($d) ."<br />";
How can I round up from number 621 to 700 in PHP?
I think you are trying to round off a number to the nearest 100.
Simply do this by using the ceil function.
ceil(621 / 100) * 100;
You can use ceil function to round any number to its nearest number.
$number = ceil($inputNumber / $nearestNumber) * $nearestNumber;
Related Topics
Using Default Arguments in a Function
How to Disable Output Buffering in PHP
Resetting Array Pointer in Pdo Results
What Do I Need to Store in the PHP Session When User Logged In
How Safe Are Pdo Prepared Statements
How to Strip a Tag and All of Its Inner HTML Using the Tag'S Id
What Is Causing "Unable to Allocate Memory For Pool" in PHP
JavaScript: How to Create Jsonp
PHP Difference Between Array() and []
How to Detect a Create, Update, Delete Query Is Successful in Codeigniter
Pdo With "Where... In" Queries
How to Replace Text Urls and Exclude Urls in HTML Tags
How to Remove Extension from String (Only Real Extension!)
PHP Sessions Timing Out Too Quickly
How to Successfully Rewrite Old MySQL-PHP Code With Deprecated MySQL_* Functions