Rounding a calculation up using php - 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;

Related

How can I create an array of a divided number in PHP?

I want to create an array in PHP by dividing a number. So for example when I have the number 200 and I divide it through 0,10, I need to get an array with 2k entries of 0,10.
But when I have a division like 233,12 / 0,10, I also need the array but the last possible entry needs to be a bit higher so that it fills the sum up.
Actually I have no code. This is too complex for me. Maybe someone has an idea. I've did everything around this but got really stuck here...
$number = 200;
$divider = 0.10;
So you want to have an array with 2k entries each having the value 0.1 in them?
So something like
$number = 200;
$divider = 0.1;
$totalArraySize = $number / $divider;
$result = [];
$result = array_fill(0, $totalArraySize, $divider);
However to make it slightly bigger in case of (for example) 233,12/0,1 you would simply need to ceil() the $totalArraySize to round it up to the nearest full integer. And to make sure that the last entry has the difference you basicly need to calculate it. You get it by taking what you expect to be the sum and subtract the values you know to be right. So (totalSize - 1) * 0,1 .. would give you in this example 233,1 so the last entry would be then 233,12 - 233,1 = 0,02
$number = 233,12;
$divider = 0.1;
$totalArraySize = ceil($number / $divider);
$wasRoundUp = $number % 1 === 0
$result = [];
$result = array_fill(0, $totalArraySize, $divider);
if ($wasRoundUp)
$result[$totalArraySize] = $number - (($totalArraySize - 1) * 0,1)
Edit: Actually I realised that my logic to see if it was round up was wrong. I cannot rely on modulo 1 division here as 233,1 would be divisible by 0.1 in an even amount. So we need to check if totalArraySize != ($number / divider).
So new code would be
$number = 233,12;
$divider = 0.1;
$totalArraySize = ceil($number / $divider);
$wasRoundUp = $totalArraySize != ($number / $divider);
$result = [];
$result = array_fill(0, $totalArraySize, $divider);
if ($wasRoundUp)
$result[$totalArraySize] = $number - (($totalArraySize - 1) * 0,1)
Edit 2:
To reflect the question in the comment and "fix" an issue with the code here's the new answer. To fill up the last entry it now takes the divider value and not hardcoded 0.1 which would have to be changed correctly. And due to floating point calculations you would need to round the last value to two decimal points. I cannot guarantee if thats the best aproach tho to avoid the floating point rounding issue you might get there
$number = 164.85;
$divider = 0.2;
$totalArraySize = ceil($number / $divider);
$wasRoundUp = $totalArraySize != ($number / $divider);
$result = array_fill(0, $totalArraySize, $divider);
if ($wasRoundUp)
$result[$totalArraySize] = round($number - (($totalArraySize - 1) * $divider), 2);

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

Easiest way to round down to nearest 50 or 00

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

PHP random decimal between two decimals with step 0.5

I want to create random number between two decimal numbers with step 0.5.
Examples: 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, ...
Use PHP To Generate Random Decimal Beteween Two Decimals
So far I can generate numbers between 0 and 5 with one decimal comma.
How to integrate step 0.5?
$min = 0;
$max = 5;
$number = mt_rand ($min * 10, $max * 10) / 10;
This should work for you:
$min = 0;
$max = 5;
echo $number = mt_rand($min * 2, $max * 2) / 2;
Another possible way:
function decimalRand($iMin, $iMax, $fSteps = 0.5)
{
$a = range($iMin, $iMax, $fSteps);
return $a[mt_rand(0, count($a)-1)];
}
More intuitive, less unnecessary actions:
$min = 0;
$max = 5;
$step = 0.5;
// Simple for the case above.
echo $number = mt_rand($min * 2, $max * 2) * $step;
More general, a bit sophisticated case
echo $number = mt_rand(floor($min / $step), floor($max / $step)) * $step;
mt_rand offical docs just in case.

Converting string to integer from form

Need a little help
I have
$_POST["zapremina"]=2000;
$_POST["starost"]="15%";
$_POST["namena"]="50%";
I want simple function to do this
$foo=(2000 - 15%) - 50%;
How to do that?
PHP is loosely typed, so you don't have to cast types explicity or do unnecessary operations (e.g. str_replace)
You can do the following:
$z = $_POST["zapremina"]; //$_POST["zapremina"]=2000;
$s = $_POST["starost"]; //$_POST["starost"]=15%;
$n = $_POST["namena"]; //$_POST["namena"]="50%;
$result = (($z - ($z *($s / 100))) - ($z * ($n / 100)));
Remember to use parentheses to have a readable code and meaningful var names.
Like this:
$starostPercentage = (substr($POST["starost"], 0, -1) / 100);
$namenaPercentage = (substr($POST["namena"], 0, -1) / 100);
$foo = ($_POST["zapremina"] * (100 - $starostPercentage)) * $namenaPercentage;
This is what this does and why:
Convert the percentages (like 15%) from their text form to their decimal form (substr(15%) = 15, 15 / 100 = 0.15).
Calculate $foo with these decimals. 2000 - 15% is what you would write (as a human), but in PHP you need to write that as 2000 * (100 * 0.15), meaning: 85% of 2000).
I'd go with this:
$zap = intval($_POST['zapremina']);
$sta = intval($_POST['starost']);
$nam = intval($_POST['namena']);
$foo = ($zap * ((100-$sta)/100)) * ((100 - $nam)/100)
add this function and then call it
function calculation($a, $b, $c)
{
$b = substr($b, 0, -1) / 100;
$c = substr($c, 0, -1) / 100;
return (($a * $b) * $c);
}
and now you can call
$foo = calculation($_POST["zapremina"], $_POST["starost"], $_POST["namena"]);
go with function most of times, because it will be helpful for reusability.

Categories