I have this if statement:
if($_GET["angle_1"] > 39) {
$markers["###ANGLE###"] = "45";
} elseif($_GET["angle_1"] > 29 && $_GET["angle_1"] < 40) {
$markers["###ANGLE###"] = "35";
} elseif($_GET["angle_1"] < 30) {
$markers["###ANGLE###"] = "25";
} else {
$markers["###ANGLE###"] = "45";
}
Is there a better / simpler way to do this check, f.x. with round that will round the integer to the nearest 5, i.e. 28 -> 25 or 34 -> 35 etc. and in that, if the integer is less than 25, it will always be 25 and if the integer is higher than 45, it will always be 45 and again if the integer is between 30 and 40 it will always be 35.
That returned value will be used to display an image.
EDIT:
I have 3 images: image_25, image_35 and image_45, therefore the need to round.
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;
Give this a try:
$var = 5 * round($n / 5);
Taken from here
Related
I want a php function which returns 55 when calling it with 52.
I've tried the round() function:
echo round(94, -1); // 90
It returns 90 but I want 95.
Thanks.
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;
}
Divide by 5
round() (or ceil() if you want to round up always)
Multiply by 5.
The value 5 (the resolution / granularity) can be anything — replaced it in both step 1 and 3
So in summary:
$rounded_number = ceil( $initial_number / 5 ) * 5
Round down:
$x = floor($x/5) * 5;
Round up:
$x = ceil($x/5) * 5;
Round to closest (up or down):
$x = round($x/5) * 5;
echo $value - ($value % 5);
I know it's an old question, but IMHO using modulus operator is the best way, and far more elegant than the accepted answer.
Try this little function I wrote.
function ceilFive($number) {
$div = floor($number / 5);
$mod = $number % 5;
If ($mod > 0) $add = 5;
Else $add = 0;
return $div * 5 + $add;
}
echo ceilFive(52);
From Gears library
MathType::roundStep(50, 5); // 50
MathType::roundStep(52, 5); // 50
MathType::roundStep(53, 5); // 55
MathType::floorStep(50, 5); // 50
MathType::floorStep(52, 5); // 50
MathType::floorStep(53, 5); // 50
MathType::ceilStep(50, 5); // 50
MathType::ceilStep(52, 5); // 55
MathType::ceilStep(53, 5); // 55
Source:
public static function roundStep($value, int $step = 1)
{
return round($value / $step) * $step;
}
public static function floorStep($value, int $step = 1)
{
return floor($value / $step) * $step;
}
public static function ceilStep($value, int $step = 1)
{
return ceil($value / $step) * $step;
}
Multiply by 2, round to -1, divide by 2.
Here is my version of Musthafa's function. This one is more complex but it has support for Float numbers as well as Integers. The number to be rounded can also be in a string.
/**
* #desc This function will round up a number to the nearest rounding number specified.
* #param $n (Integer || Float) Required -> The original number. Ex. $n = 5.7;
* #param $x (Integer) Optional -> The nearest number to round up to. The default value is 5. Ex. $x = 3;
* #return (Integer) The original number rounded up to the nearest rounding number.
*/
function rounduptoany ($n, $x = 5) {
//If the original number is an integer and is a multiple of
//the "nearest rounding number", return it without change.
if ((intval($n) == $n) && (!is_float(intval($n) / $x))) {
return intval($n);
}
//If the original number is a float or if this integer is
//not a multiple of the "nearest rounding number", do the
//rounding up.
else {
return round(($n + $x / 2) / $x) * $x;
}
}
I tried the functions from Knight, Musthafa and even the suggestion from Praesagus. They don't have support for Float numbers and the solutions from Musthafa's & Praesagus do not work correctly in some numbers. Try the following test numbers and do the comparison yourself:
$x= 5;
$n= 200; // D = 200 K = 200 M = 200 P = 205
$n= 205; // D = 205 K = 205 M = 205 P = 210
$n= 200.50; // D = 205 K = 200 M = 200.5 P = 205.5
$n= '210.50'; // D = 215 K = 210 M = 210.5 P = 215.5
$n= 201; // D = 205 K = 205 M = 200 P = 205
$n= 202; // D = 205 K = 205 M = 200 P = 205
$n= 203; // D = 205 K = 205 M = 205 P = 205
** D = DrupalFever K = Knight M = Musthafa P = Praesagus
I do it like this:
private function roundUpToAny(int $n, $x = 9)
{
return (floor($n / 10) * 10) + $x;
}
Tests:
assert($this->roundUpToAny(0, 9) == 9);
assert($this->roundUpToAny(1, 9) == 9);
assert($this->roundUpToAny(2, 9) == 9);
assert($this->roundUpToAny(3, 9) == 9);
assert($this->roundUpToAny(4, 9) == 9);
assert($this->roundUpToAny(5, 9) == 9);
assert($this->roundUpToAny(6, 9) == 9);
assert($this->roundUpToAny(7, 9) == 9);
assert($this->roundUpToAny(8, 9) == 9);
assert($this->roundUpToAny(9, 9) == 9);
assert($this->roundUpToAny(10, 9) == 19);
assert($this->roundUpToAny(11, 9) == 19);
assert($this->roundUpToAny(12, 9) == 19);
assert($this->roundUpToAny(13, 9) == 19);
assert($this->roundUpToAny(14, 9) == 19);
assert($this->roundUpToAny(15, 9) == 19);
assert($this->roundUpToAny(16, 9) == 19);
assert($this->roundUpToAny(17, 9) == 19);
assert($this->roundUpToAny(18, 9) == 19);
assert($this->roundUpToAny(19, 9) == 19);
function round_up($n, $x = 5) {
$rem = $n % $x;
if ($rem < 3)
return $n - $rem;
else
return $n - $rem + $x;
}
I just wrote this function in 20 min, based on many results I found here and there, I don't know why it works or how it works!! :D
I was mainly interested in converting currency numbers from this 151431.1 LBP to 150000.0 LBP. (151431.1 LBP == ~100 USD) which works perfectly so far, however I tried to make it somehow compatible with other currencies and numbers, but not sure if it works fine!!
/**
* Example:
* Input = 151431.1 >> return = 150000.0
* Input = 17204.13 >> return = 17000.0
* Input = 2358.533 >> return = 2350.0
* Input = 129.2421 >> return = 125.0
* Input = 12.16434 >> return = 10.0
*
* #param $value
* #param int $modBase
*
* #return float
*/
private function currenciesBeautifier($value, int $modBase = 5)
{
// round the value to the nearest
$roundedValue = round($value);
// count the number of digits before the dot
$count = strlen((int)str_replace('.', '', $roundedValue));
// remove 3 to get how many zeros to add the mod base
$numberOfZeros = $count - 3;
// add the zeros to the mod base
$mod = str_pad($modBase, $numberOfZeros + 1, '0', STR_PAD_RIGHT);
// do the magic
return $roundedValue - ($roundedValue % $mod);
}
Feel free to modify it and fix it if there's anything wrong
Probably you can also consider this one liner. It's faster! Works for $num >= 0 and $factor > 0.
$num = 52;
$factor = 55;
$roundedNum = $num + $factor - 1 - ($num + $factor - 1) % $factor;
i want to round up a number like this
1439 to 1400
1399 to 1350
What are the nearest way to do this in php?
Given the new examples...
Looks like you want to use PHP floor instead and apply the 50 yourself
50 * floor($number / 50)
OLD ANSWER
Going from your examples, rather than the question title..
Try the PHP round function.
In your case:
round($number, -2);
The second param is the number of decimal figures to round to, negative values go to the left of the 'ones' digit instead.
There is also a third parameter for some more subtle variations.
$rounded_n1 = round($n1 / 50, 0) * 50;
You can do something like that (only to round down) :
$n1 = 1439;
$n2 = 1399;
$round1 = $n1 - $n1 % 50; // round1 = 1439 - 39 = 1400
$round2 = $n2 - $n2 % 50; // round2 = 1399 - 49 = 1350
To round up, you can do this :
$n1 = 1439;
$n2 = 1399;
$round1 = $n1 + (50 - $n1 % 50); // round1 = 1439 + (50 - 39) = 1450
$round2 = $n2 + (50 - $n2 % 50); // round2 = 1399 + (50 - 49) = 1400
You can do it like:
Divide it by 100.
Truncate.
Multiply by 100.
This is the best thing I could come up with.
$num = 1401;
$num /= 100;
$num = round($num);
$num *= 100;
Use ceil to always round up
$round1 = ceil($n1/ 50) * 50
You should try:
function specialRoundUp($val) {
return 100 * round($val / 100);
}
I'm looking for a formula to round a value to nearest 5 or 9 if the val is less than 5 make 5 if is bigger than 5 make 9.
Example:
$RoundToFive = ceil('232' / 5) * 5;
echo floor($RoundToFive * 2 ) / 2; //Result is 235 Is good
$RoundToNine = ceil('236' / 5) * 5;
echo floor($RoundToNine * 2 ) / 2; //Result is 240 but i need 239
Is there a way to extract always the last 2 digits and convert to 5 or nine ?
Any help is appreciated !
how about:
function funnyRound($number){
$rounded = ceil($number / 5) * 5;
return $rounded%10?$rounded:$rounded-1;
}
This is working
<?php
function roundToDigits($num, $suffix, $type = 'floor') {
$pow = pow(10, floor(log($suffix, 10) + 1));
return $type(($num - $suffix) / $pow) * $pow + $suffix;
};
$RoundToNine = ceil('236' / 5) * 5;
echo roundToDigits($RoundToNine,5);
echo roundToDigits($RoundToNine,9);
You can use any number as $suffix to round to it.
other way, working with strings... :
<?php
function round59($NUMB){
//cast the value to be Int
$NUMB = intval($NUMB);
//Get last number
$last_number = intval(substr($NUMB, -1));
$ROUND_NUMBER = 5;
if($last_number<=5)
$ROUND_NUMBER = 5;
else
$ROUND_NUMBER = 9;
//Remove Last Character
$NUMB = substr($NUMB, 0, -1);
// now concat the results
return intval($NUMB."".$ROUND_NUMBER) ;
}
echo round59(232);
echo round59(236);
?>
I'm trying to reproduce the following as a one-liner.
if($l < 10) $next = 5; return;
if($l < 20) $next = 10; return;
if($l < 30) $next = 15; return;
if($l < 40) $next = 20; return;
if($l < 50) $next = 25; return;
if($l < 60) $next = 30; return;
if($l < 70) $next = 35; return;
if($l < 80) $next = 40; return;
if($l < 90) $next = 45; return;
if($l < 100) $next = 50; return;
(not syntactically correct but you get the idea)
So that if the number is less than 10, $next is 5, and if the number is less than 20 then it's 10.
$next = ((round($l, -1)-5)); is as close as I can get to it but that gives
5
15
25
35
45
55
65
75
85
not the desired 5, 10, 15, 20 .. etc
What is the correct way to write this?
add 10 to your number, then divide the result by 10 and round it down to the nearest (floor) integer, you will then have the number by which to multiply 5, which will yield your result... so...
let's say your number is 47.
47 + 10 = 57
57 / 10 = 5.7
floor 5.7 = 5
5x5 = 25
return floor(($i + 10)/10) * 5
(($l + 10) / 10) * 5 will do the trick
DIVDIDE through ten (and round that to int), then multiply that with 5 ... 100/10=10 => 10*5=50
This might do:
$i=10;
while($l < $i){
$next = $i / 2;
$i+=10;
}
If I understand your question correctly, it's not harder than;
$next = floor($l/10)*5+5;
I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.
how can I do that ?
if ($number % 6 != 0) {
$number += 6 - ($number % 6);
}
The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.
If decreasing is acceptable then this is even faster:
$number -= $number % 6;
if ($variable % 6 == 0) {
echo 'This number is divisible by 6.';
}:
Make divisible by 6:
$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
$num += (6-$num%6)%6;
no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.
So you want the next multiple of 6, is that it?
You can divide your number by 6, then ceil it, and multiply it again:
$answer = ceil($foo / 6) * 6;
I see some of the other answers calling the modulo twice.
My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.
Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.
Code: (Demo)
$factor = 6;
for($x = 0; $x < 10; ++$x){ // battery of 10 tests
$number = rand( 0 , 100 );
echo "Number: $number Becomes: ";
if( $remainder = $number % $factor ) { // if not zero
$number += $factor - $remainder; // use cached $remainder instead of calculating again
}
echo "$number\n";
}
Possible Output:
Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66
Use the Mod % (modulus) operator
if ($x % 6 == 0) return 1;
function nearest_multiple_of_6($x) {
if ($x % 6 == 0) return $x;
return (($x / 6) + 1) * 6;
}
Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.
while ($number % 6 != 0) {
$number++;
}
Assuming $foo is an integer:
$answer = (int) (floor(($foo + 5) / 6) * 6)
For micro-optimisation freaks:
if ($num % 6 != 0)
$num += 6 - $num % 6;
More evaluations of %, but less branching/looping. :-P
Why don't you use the Modulus Operator?
Try this:
while ($s % 6 != 0) $s++;
Or is this what you meant?
<?
$s= <some_number>;
$k= $s % 6;
if($k !=0) $s=$s+6-$k;
?>
result = initial number + (6 - initial number % 6)