Easiest way to round down to nearest 50 or 00 - php

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);
}

Related

How can I round up from number 621 to 700 in PHP?

$num=621;
echo round(round(621,-2));
Something like this (returns the closest $multiple not less than the given $number):
function round_up($num, $mul) {
return ceil($num / $mul) * $mul;
}
Called like this:
echo round_up(621, 100);
> 700
Works for "weird" quantities, too:
echo round_up(124.53, 0.25);
> 124.75
echo round_up(pi(), 1/7);
> 3.1428571428571
If you want to specify decimal places instead of multiples, you could use the power operator ** to convert decimal places to multiples.
You could do round_down in a similar way using floor.
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;
As an option, subtract the reminder and add 100:
function ceil100($value) {
return $value - $value % 100 + 100;
}
My answer is $numero = $numero + 100 - $numero % 100;
php > $numero = 621;
php > $numero = $numero + 100 - $numero % 100;
php > echo $numero;
700

Rounding a calculation up using php

How would I round this calculation up automatically??
$calc = (14.3/10)/2.33 Result = 6.137339055794
The value I would like to have is 6.13
Have searched the site but can't find any answers on this
Use PHP round().
$calc = round($calc, 2)
The result of (14.3/10)/2.33 is not 6.137339055794. It's 0.6137339055794.
I'm assuming: $calc = (14.3/10) / 2.33 * 10;.
The function round rounds half up, half down, half even or half odd numbers (floating point numbers), but the result of round($calc, 2, PHP_ROUND_HALF_UP) is not 6.13. It's 6.14.
Assuming you'd like to round down or truncate that number, a solution could be:
$truncated = (int)($calc * 100) / 100;
or
$precision = 2;
$tensPrecision = pow(10, $precision);
$truncated = (int)($calc * $tensPrecision) / $tensPrecision;
.
To round up, you could do:
$truncated = ceil($calc * 100) / 100;
or
$precision = 2;
$tensPrecision = pow(10, $precision);
$truncated = ceil($calc * $tensPrecision) / $tensPrecision;

round to nearest multiple of 5 in a range in php

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

Algorithm to portion

I have a number X, consider X = 1000
And I want piecemeal this number at three times, then Y = 3, then X = (X / 3)
This will give me equal, just not always accurate, so I need: a percentage value is set, also consider K = 8, K is the percentage, but what I want to do? I want the first portion has a value over 8% in K, suppose that 8% are: 500 and the other two plots are 250, 250
The algorithm is basically what I need it, add a percentage value for the first installment and the other equals
EDIT
I just realized, this is far simpler than I made it. To find the value of $div in my original answer you can just:
$div = (int)($num / ($parcels + $percent / 100));
Then the $final_parcels will be the same as below. Basically, the line above replaces the while loop entirely. Don't know what I was thinking.
/EDIT
I think this will do what you want... unless I am missing something.
<?php
$num = 1000;
$percent = 8;
$parcels = 3;
$total = PHP_INT_MAX;
$div = (int)($num / $parcels);
while ($total > $num) {
$div -= 1;
$total = (int)($div * ($parcels + $percent / 100));
}
$final_parcels = array();
$final_parcels[] = ($num - (($parcels - 1) * $div));
for ($i = 1; $i < $parcels; $i++) {
$final_parcels[] = $div;
}
print_r($final_parcels);
This output will be
Array
(
[0] => 352
[1] => 324
[2] => 324
)
324 * 1.08 = 350.
352 + 324 * 2 = 1000.
Let $T is your total X, $n is a number of parts and $K is percentage mentioned above. Than
$x1 = $T / $n + $T * $K / 100;
$x2 = $x3 = .. = $xn = ($T - $x1) / ($n - 1);
Applied to your example:
$x1 = 1000 / 3 + 1000 * 0.03 = 363.3333333333333333333333333333
// you could round it if you want
// lets round it to ten, as you mentioned
$x1 = round($x1, -1) = 360
$x2 = $x3 = (1000 - 360) / 2 = 320
Extra for the first piece W = X*K/100
Remaining Z = X-W
Each non-first piece = Z/Y = (X-W)/Y = (100-K)*X/(100*Y)
The first piece = W + (100-K)*X/(100*Y) = X*K/100 + (100-K)*X/(100*Y)

how to round up to the next x20 number with php?

I know ceil, which will round 15.1 to 16 and 31.2 to 32.
But how to round up to the next x20 number? like 15.1 to 20 and 31,2 to 40?
Is needed to make sensefull labels for the y-axis of a chart.
Try this (works in JavaScript):
$result = 20 * ceil($input / 20);
Here, we're rounding to 20. To round to other numbers, simply replace 20 with whatever base you want. The documentation for ceil() can be found here.
A function that does the same:
function roundTo($value, $base)
{
return $base * ceil($value / $base);
}
As a small aside, if you want to round to the nearest base, instead of rounding up, use round() instead of ceil().
divide by 20 and use ceil, then multiply by 20
Or you could use modulus:
echo round($a + 20 - ($a % 20));
If you want "mid-range" values to round more, you can use the following.
function roundTo($n,$i = 1){
$r = $n % $i;
$d = round(($n - $r * $i) / $i);
return $r * $i + $d * $i;
}
for ($a = 0; $a < 100; $a++){
printf("%d = %d\r\n", $a, ceil2($a, 20));
}
The above produces:
Number: Rounded To:
0-10 0
11-30 20
31-50 40
etc.
This more closely simulates how 1.3=1 but 1.5=2.
you can use this
ceil(x/2)*2

Categories