PHP round() 0.50 to lower value - php

is there any way you can round to lower with any number lower than .50 including .50?
For example:
round(1.49) => 1
round(1.51) => 2
round(1.50) => 2
Is there any way to make it like this:
round(1.49) => 1
round(1.51) => 2
round(1.50) => 1

Use PHP_ROUND_HALF_DOWN flag
echo round(1.49, 0, PHP_ROUND_HALF_DOWN);
echo PHP_EOL;
echo round(1.50, 0, PHP_ROUND_HALF_DOWN);
echo PHP_EOL;
echo round(1.51, 0, PHP_ROUND_HALF_DOWN);

Subtract 0.5 from your number and use ceil on the result.
E.g. ceil(1.51 - 0.5) = 2, ceil(1.50 - 0.5) = 1
It's a perversion of the idiomatic way of doing rounding without a round function. The subtraction caters for your wanting to round the 1/2 way point downwards. Extra checks necessary for negatives.

Related

PHP round number with 2 digits to the nearest float number

How can I round 2 digits number to the nearest number using php, to be clear like the example below
1.06 => 1.1
1.03 => 1.1
1.02 => 1.0
so if the third number greater or equal to 3 then round to 1.1 and so on
$mynumber=1.03;
$mynumber_exp=explode(".",$mynumber);
$last_digit=substr("$mynumber", -1);
if ($last_digit >= 3) {
$rounded_number = "$mynumber_exp[0].1";
echo "$rounded_number"; // PRINT .1
}
else {
$rounded_number = "$mynumber_exp[0].0";
echo "$rounded_number"; // PRINT .0
}
You can use round(), but you need to add 0.02 to make it round 0.03 up and anything below is rounded down.
function round3( $in ) {
return round($in+0.02, 1);
}
echo round3(1.06).PHP_EOL;
echo round3(1.03).PHP_EOL;
echo round3(1.02);
gives
1.1
1.1
1

Use round or floor depending the number after the comma

What would be the solution to have:
7.1 => 7
7.5 => 7
7.8 => 8
So I need to round number or floor depending on the number after the comma.
How to do that?
Thanks.
You should be able to use the constant, PHP_ROUND_HALF_DOWN, to have the round function round down when it is half way.
echo round(7.1, 0, PHP_ROUND_HALF_DOWN) . "\n";
echo round(7.5, 0, PHP_ROUND_HALF_DOWN) . "\n";
echo round(7.8, 0, PHP_ROUND_HALF_DOWN) . "\n";
Output:
7
7
8
From the manual:
Round val down to precision decimal places towards zero, when it is half way there. Making 1.5 into 1 and -1.5 into -1.
PHP Demo: https://eval.in/427706
One way to do this is to split the value at the decimal (or comma? your example is using decimals) and test the trailing number to see whether you want to use floor or ceiling.
$test = 7.6
$arrayTest = explode(".",$test);
if(isset($arrayTest[1]) && $arrayTest[1] > 5) {
//do something
} else {
//do something else
}

How can I round up a number without php function ceil

I'm trying to build a function that output will be rounded up number. I know there is a php function, but I want to make this function for another purpose.
you want ceil without using ceiling...
intval($number + .5)
this is the same thing, but you are still using a built in function.
EDIT: apparently the above solution does not work as I intended it to in PHP. You can use the round function to similar effect
round($number + .5)
or something similar to another answer:
$n = intval($number + .5);
if($n < $number){
$n++;
}
May this do it?
function newceil($num)
{
$re=intval($num);
if($re<$num) $re++;
return $re
}
You could cut off the fractional part by casting it to an integer and afterwards check, whether the so derived value is smaller or even the initial value.
$input = 3.141592653;
$intVersion = (int) $input;
if($intVersion<$input) $intVersion++;
return $intVersion
If you want to round up/down
You can use round method
/* Using PHP_ROUND_HALF_UP with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_UP); // 1.6
echo round( 1.54, 1, PHP_ROUND_HALF_UP); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_UP); // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_UP); // -1.5
/* Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_DOWN); // 1.5
echo round( 1.54, 1, PHP_ROUND_HALF_DOWN); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_DOWN); // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_DOWN); // -1.5
/* Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_EVEN); // 1.6
echo round( 1.54, 1, PHP_ROUND_HALF_EVEN); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_EVEN); // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_EVEN); // -1.5
/* Using PHP_ROUND_HALF_ODD with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_ODD); // 1.5
echo round( 1.54, 1, PHP_ROUND_HALF_ODD); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_ODD); // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_ODD); // -1.5
?>
Note: ceil round up
You can use round() and floor() and number_format() for round up number.
echo round(153.751); // 154
echo floor(153.751); // 153
echo number_format(153.751); // 154

Rounding in PHP to achieve 100%

I need to total the number of clicks over 10 links on my page and then figure out the percentage of people that clicked each. This is easy division, but how do I make sure that I get a round 100% at the end.
I want to use the below code, but am worried that a situation could arise where the percentages do not tally to 100% as this function simply removes the numbers after the period.
function percent($num_amount, $num_total) {
$count1 = $num_amount / $num_total;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
echo $count;
}
Any advice would be much appreciated.
Instead of calculating one percentage in your function you could pass all your results as an array and process it as a whole. After calculating all the percentages and rounding them make a check to see if they total 100. If not, then adjust the largest value to force them all to total 100. Adjusting the largest value will make sure your results are skewed as little as possible.
The array in my example would total 100.02 before making the adjustment.
function percent(array $numbers)
{
$result = array();
$total = array_sum($numbers);
foreach($numbers as $key => $number){
$result[$key] = round(($number/$total) * 100, 2);
}
$sum = array_sum($result);//This is 100.02 with my example array.
if(100 !== $sum){
$maxKeys = array_keys($result, max($result));
$result[$maxKeys[0]] = 100 - ($sum - max($result));
}
return $result;
}
$numbers = array(10.2, 22.36, 50.10, 27.9, 95.67, 3.71, 9.733, 4.6, 33.33, 33.33);
$percentages = percent($numbers);
var_dump($percentages);
var_dump(array_sum($percentages));
Output:-
array (size=10)
0 => float 3.51
1 => float 7.69
2 => float 17.22
3 => float 9.59
4 => float 32.86
5 => float 1.28
6 => float 3.35
7 => float 1.58
8 => float 11.46
9 => float 11.46
float 100
This will also work with an associative array as the function parameter. The keys will be preserved.
These figures could now be presented in a table, graph or chart and will always give you a total of 100%;
What you want to do is this.
Total the number of clicks across the board, then divide each number by the total.
For example:
1134
5391
2374
2887
In this case, four buttons, with a total of 11786 clicks, so:
1134 / 11786 = 0.09621....
5391 / 11786 = 0.45740....
2374 / 11786 = 0.20142....
2887 / 11786 = 0.24495....
Then for each division, round the result to 'two decimal points', so the first result:
0.09621.... becomes 0.10
because the 3rd point is 5 or above, it would remain at 0.09 if the 3rd point was below 5.
Once you have all of the results rounded, multiply each by 100 then add them up.
The ending result will always be 100.
Should warn you however that depending on how you use each individual percentage, when you round them, any result less that 0.05 will become 0%, unless you keep the value before you round it so you can declare it as a percentage less than 1.
I think you want to use ceil() or round() .
Since these are floating point numbers, there is room for error. Be careful how you round, and be sure that you don't independently calculate the last remaining percentages. Simply subtract the total of what you have from 1 or 100.
Make sure you dont calculate separate sides of the equation, sum one side, then subtract the other from 1 or 100 or however you are handling your percentages.
I run into this quite a bit and have a hack for it.
$percentages = array(
'1' => 87.5,
'2' => 12.5,
'3' => 0,
'4' => 0,
'5' => 0
);
If you round those percentages for output, you will end up with 88% and 13% (101%)
round($percentages['1']);
round($percentages['2']);
// 88
// 13
So here is the code I use to fix it.
$checkTotal = array_sum($percentages);
$max = max(array_keys($percentages));
if ($checkTotal > 100) {
$percentages[$max] = $percentages[$max] - 1;
}
if ($checkTotal < 100) {
$percentages[$max] = $percentages[$max] + 1;
}
If it is 100, do nothing.
If it is less than 100, add 1 to equal 100
If it is over 100, subtract 1 to equal 100

What's happening with PHP's intval() and strval()?

So I got this:
$n = "19.99";
echo (int) ($n * 100);
echo (int) strval($n * 100);
That outputs:
1998
1999
And I just don't get it. What happened? $n * 100 is 1999 (float), why aren't they the same then?
Floats don't have arbitrary precision. The result of 19.99 * 100 is actually represented as 1998.9999999999.
If you print that, it'll show as 1999. That's what strval() does. Whereas (int) will floor it down.
Also check...
http://php.net/manual/en/function.ceil.php
http://php.net/manual/en/function.floor.php
http://php.net/manual/en/function.round.php
You will see how everything works.
echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10
echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9
echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9
echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9
To multiply two arbitrary precision numbers use bcmul function.
echo (int)(bcmul('19.99', '100')); // 1999
echo (int)(19.99 * 100); // 1998
Note that the two operands of bcmul function are strings because according to php.net :
Passing values of type float to a BCMath function which expects a string as operand may not have the desired effect due to the way PHP converts float values to string, namely that the string may be in exponential notation (what is not supported by BCMath), and that the decimal separator is locale dependend (while BCMath always expects a decimal point).

Categories