$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
Related
I have a question and I don't know what is the correct term to use in search!
what PHP function should I use to print the number like this
Original number > Expected result
15001 > 15000
16300 > 16000
22700 > 22000
I mean to remove all numbers after the thousands
any help ?
Just some math:
Divide the $number by 1000, round fractions down with floor or ceil, then multiply it by 1000.
$number = 22700;
// if ($number < 1000) { /* don't apply this */ }
echo floor($number / 1000) * 1000; // 22000
// or
echo ceil($number / 1000) * 1000; // 23000
One way to rome:
foreach([1,12,123,1234,12345,123456,1234567] as $num){
print $num. " > ". (substr($num,-1*strlen($num),-3)."000") . '<br>';
}
//result:
//1 > 000
//12 > 000
//123 > 000
//1234 > 1000
//12345 > 12000
//123456 > 123000
//1234567 > 1234000
No rounding here. Just replacing.
I am trying to round down numbers using PHP.
I have managed to do this if the value has a decimal place, using this method below.
$val = floor($val * 2) / 2;
echo 'hello'. $val;
If the value I am trying to round down doesn't have a decimal place and the above code is not working.
The values I am trying to round down.
32456 => 32000
4567 => 4000
38999 => 38000
There are a few ways to do this. The most common way (for rounding down to the nearest 1000) would be something like this:
function roundDown1000($n)
{
return floor($n / 1000) * 1000;
}
More generally:
function roundDown($n, $increment)
{
return floor($n / $increment) * $increment;
}
If you wanted, you could also do $n - ($n % 1000), but this will get weird results for $n < 0.
I need some help. It's a simple code, but I don't have idea how to write in down. I have the numbers:
$NumberOne = 500;
$NumberTwo = 430;
$NumberThree = 150;
$NumberFour = 30;
At all this is:
$Everything = 1110; // all added
Now I want to show what percentage is for example $NumberFour of everything or what percentage is $NumberTwo of $Everything. So the "market share".
Use some simple maths: divide the number you wish to find the percentage for by the total and multiply by 100.
Example:
$total = 250;
$portion = 50;
$percentage = ($portion / $total) * 100; // 20
Solution to original example
To get $NumberFour as a percentage of the total amount you'd use:
$percentage = ($NumberFour / $Everything) * 100;
Rounding
Depending on the numbers you're working with, you may want to round the resulting percentage. In my initial example, we get 20% which is a nice round number. The original question however uses 1110 as the total and 30 as the number to calculate the percentage for (2.70270...).
PHP's built in round() function could be useful when working with percentages for display: https://www.php.net/manual/en/function.round.php
echo round($percentage, 2) . '%'; // 2.7% -- (30 / 1110) * 100 rounded to 2dp
Helper Functions
I would only contemplate creating helper functions when their use justifies it (if calculating and displaying a percentage isn't a one-off). I've attached an example below tying together everything from above.
function format_percentage($percentage, $precision = 2) {
return round($percentage, $precision) . '%';
}
function calculate_percentage($number, $total) {
// Can't divide by zero so let's catch that early.
if ($total == 0) {
return 0;
}
return ($number / $total) * 100;
}
function calculate_percentage_for_display($number, $total) {
return format_percentage(calculate_percentage($number, $total));
}
echo calculate_percentage_for_display(50, 250); // 20%
echo calculate_percentage_for_display(30, 1110); // 2.7%
echo calculate_percentage_for_display(75, 190); // 39.47%
Create function to calculate percentage between two numbers.
<?php
/**
* Calculate percetage between the numbers
*/
function percentageOf( $number, $everything, $decimals = 2 ){
return round( $number / $everything * 100, $decimals );
}
$numbers = array( 500, 430, 150, 30 );
$everything = array_sum( $numbers );
echo 'First of everything: '.percentageOf( $numbers[0], $everything )."%\n";
echo 'Second of everything: '.percentageOf( $numbers[1], $everything )."%\n";
echo 'Third of everything: '.percentageOf( $numbers[2], $everything )."%\n";
echo 'Fourth of everything: '.percentageOf( $numbers[3], $everything )."%\n";
?>
This outputs
First of everything: 45.05%
Second of everything: 38.74%
Third of everything: 13.51%
Fourth of everything: 2.7%
You can also come up with the percentage with the following formula. For the percentage, add a 0. in front of it. So 5% would be 0.05. Total X 0.05 is the amount.
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;
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