xx.01 and xx.02 go to xx.00
xx.03 and xx.04 go to xx.05
xx.06 and xx.07 go to xx.05
xx.08 and xx.09 go to xx.10
xx.11 and xx.12 go to xx.10
xx.13 and xx.14 go to xx.15
I need the below format behind the dot.
0.05 / 0.10 / 0.15/ 0.20 / 0.25 / 0.30 / 0.35 / 0.40 etc….
Can anyone give me a function in PHP to convert the number after the dot to the expected value?
function soRound($a, $to=0.05) {
return round($a / $to) * $to ;
}
This rounds as you describe with no default second argument
i.e soRound(1.07); returns 1.05
You need to round, but you can only use round to round to the nearest tenth. You want to round to the nearest twentieth. The solution is to multiply by 2, round to the nearest tenth, divide by 2, and format as needed:
$data = [0, 0.01, 0.07, 0.09, 1.56, 1.73, 3.14159];
foreach ($data as $num) {
$num = round($num * 2, 1) / 2;
echo number_format($num, 2) . "\n";
}
Output:
0.00
0.00
0.05
0.10
1.55
1.75
3.15
Here's a working demo.
In function form:
function roundToNearest05($num) {
return round($num * 2, 1) / 2;
}
// or, more generically, this:
function roundTo($num = 0, $nearest = 0.05) {
return round($num / $nearest) * $nearest;
}
Related
How to extract the percentage from the array
The result is as follows: 41 - 16 - 8 - 33
Total is 98, not 100
How to make it = 100
$sum = array(500.36,200.32,100.09,400);
$total = array_sum($sum);
foreach($sum as $val){
$st = intval($val / $total * 100 );
echo $st.'<br>';
}
The reason is: precision :)
With intval() you skip the decimals. so 41+16+8+33 is REALLY 98.
If you add them with 2 decimals:
41.66 + 16.68 + 8.33 + 33.31 = 99.98
If you do round() instead of intval() you'll round the values so it'll be more close statistically. you'll get: 42 + 17 + 8 + 33 = 100
BUT! if you want to make sure the sum is 100, than you should pick one number (I suggest the biggest one) to calculate that as: 100 - sum(the rest).
Rounding!
Take the number 500.36;
500.36 / 1200.77 * 100:
41,669928463
Since you're using intval those deciamals are lost, remove intval to keep the decimals, then you'll reach 100 total.
Consider an extra variable to check this;
<?php
$sum = array(500.36,200.32,100.09,400);
$total = array_sum($sum);
$test = 0;
foreach($sum as $val){
$st = $val / $total * 100;
echo $st.'<br>';
$test += $st;
}
echo $test;
41.66992846257
16.682628646618
8.3354847306312
33.311958160181
100
Try it online!
you are using intval(). you should use round() with the precision of 2. you are skipping fractions by converting answer to integer. PHP is using floor method so your answer is losing fractions. here is the code
$sum = array(500.36,200.32,100.09,400);
$total = array_sum($sum);
foreach($sum as $val){
$st = round($val / $total * 100, 2 );
echo $st.'<br>';
}
If you make an intval to the result you cut the last values.
Without intval you can see the problem.
41.66992846257
16.682628646618
8.3354847306312
33.311958160181
So work with that numbers or round it on 2 decimal numbers.
$st = number_format($val / $total * 100, 2, '.', '');
something like this.
I have this code
<?php echo round(0.572,2,PHP_ROUND_HALF_UP);?>
I want to round two decimals to half up, I expect a value as 0.58...
but the above code print 0.57
How can I do this?
if you expect 0,58 you don't have to use a "half round" but the ceil function
$v = 0.575;
echo ceil($v * 100) / 100; // show 0,58
The value 0.572 can not be rounded up to 0.58, because the third decimal, 2, is less than half (or 5). If you were doing round(0.575, 2, PHP_ROUND_HALF_UP) you would get 0.58. In this case 0.57 is the correct rounded value.
If you wish to always round up from the 3rd decimal, regardless of its value you could use ciel() instead, but it requires a little additional math. A simple function to demonstrate rounding up always would be...
function forceRoundUp($value, $decimals)
{
$ord = pow(10, $decimals);
return ceil($value * $ord) / $ord;
}
echo forceRoundUp(0.572, 2); // 0.58
echo forceRoundUp(0.57321, 4); // 0.5733
function round_up($value, $places)
{
$mult = pow(10, abs($places));
return $places < 0 ?
ceil($value / $mult) * $mult :
ceil($value * $mult) / $mult;
}
echo round_up(0.572,2);
Hope this will work for you!!
I'm currently dividing two values in order to get the percentage from the actual count and the total count for my application.
I am using the following formula:
echo ($fakecount / $totalcount) * 100;
That is giving me a value like: 79.2312313. I would perefer a value like 79%.
I have tried the following:
echo round($fakecount / $totalcount) * 100;
this doesn't seem to work correctly.
Any suggestions?
You need to multiply by 100 before you round, not after:
echo round($fakecount * 100 / $totalcount);
You are calculating $fakecount / $totalcount, which will be a number between 0 and 1, then you round that, so you get either 0 or 1, then you multiply by 100, giving either 0 or 100 for your percentage.
try,
echo (int)($fakecount * 100 / $totalcount + .5);
This works because adding 0.5 increases the integer part by 1 if the decimal part is >= .5
or,
round ($fakecount * 100 / $totalcount);
Note that, I'm doing the multiplication by 100 before the division to better preserve the precision.
echo intval(($fakecount / $totalcount) * 100);
Or you can use
echo floor(($fakecount / $totalcount) * 100); //Round down
Or
echo ceil(($fakecount / $totalcount) * 100); // Round Up
use the round() function
echo round(($fakecount / $totalcount) * 100);
http://php.net/manual/en/function.round.php
others you can use are
ceil() - Round fractions up
floor() - Round fractions down
Nope...
There is a lot of ways to do it.
$mypercent = ($fakecount / $totalcount) * 100;
Remember.. this will first run what is inside of (xxx) and after will * 100.
After this...
echo round($mypercent);
And you can use a lot of rules on that too if you want...
<i>if ($value < 10) {
$value = floor($value);
} else {
$value = round($value);
}</i>
Dont forget to see that other to commands too, maybe you are going to need it.
ceil() - Round fractions up
floor() - Round fractions down
=)
So, I need to create the following functions but my head can't think of any possibility in PHP without complicated math.
Round always up to the nearest decimal (1.81 = 1.90, 1.89 = 1.90, 1.85 = 1.90)
Round always down to the nearest decimal (1.81 = 1.80, 1.89 = 1.80, 1.85 = 1.80)
Round always up to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 2, 1.32 = 1.50)
Round always down to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 1.75, 1.32 = 1.25)
Round always up to the nearest x.50 / 1 (1.23 = 1.50, 1.83 = 2)
Round always down to the nearest x.50 / 1 (1.23 = 1, 1.83 = 1.50)
I have searched on Google for 2 hours now and the only things that came up were Excel forums. Is it possible with some simple lines of PHP?
Since you're looking for fourths (.00, .25, .50, .75), multiply your number by 4, round to nearest whole number as desired (floor if down, ceil if up), then divide by 4.
1.32, down to nearest fourth:
1.32 * 4 = 5.28
floor(5.28) = 5.00
5.00 / 4 = 1.25
Same principle applies for any other fractions, such as thirds or eighths (.0, .125, .25, .375, .5, .625, .75, .875). For example:
1.77, up to nearest eighth:
1.77 * 8 = 14.16
ceil(14.16) = 15.00
15.00 / 8 = 1.875
Just for fun, you could write a function like this:
function floorToFraction($number, $denominator = 1)
{
$x = $number * $denominator;
$x = floor($x);
$x = $x / $denominator;
return $x;
}
echo floorToFraction(1.82); // 1
echo floorToFraction(1.82, 2); // 1.5
echo floorToFraction(1.82, 3); // 1.6666666666667
echo floorToFraction(1.82, 4); // 1.75
echo floorToFraction(1.82, 9); // 1.7777777777778
echo floorToFraction(1.82, 25); // 1.8
Please note that the answer isn't really water tight. Since we're dealing with floats here it's not guaranteed that when you divide the rounded number by the denominator it returns a neatly round number. It may return 1.499999999999 instead of 1.5. It's the nature of floating point numbers.
Another round is needed before returning the number from the function.
Just in case someone lands here from google like I did :)
According to the mround() function in Excel:
function MRound($num,$parts) {
$res = $num * $parts;
$res = round($res);
return $res /$parts;
}
echo MRound(-1.38,4);//gives -1.5
echo MRound(-1.37,4);//gives -1.25
echo MRound(1.38,4);//gives 1.5
echo MRound(1.37,4);//gives 1.25
Look at example #3 on here and it is half of your solution - http://php.net/manual/en/function.round.php
I'm creating this rating system using 5-edged stars. And I want the heading to include the average rating. So I've created stars showing 1/5ths. Using "1.2" I'll get a full star and one point on the next star and so on...
But I haven't found a good way to round up to the closest .2... I figured I could multiply by 10, then round of, and then run a switch to round 1 up to 2, 3 up to 4 and so on. But that seems tedious and unnecessary...
round(3.78 * 5) / 5 = 3.8
A flexible solution
function roundToNearestFraction( $number, $fractionAsDecimal )
{
$factor = 1 / $fractionAsDecimal;
return round( $number * $factor ) / $factor;
}
// Round to nearest fifth
echo roundToNearestFraction( 3.78, 1/5 );
// Round to nearest third
echo roundToNearestFraction( 3.78, 1/3 );
function round2($original) {
$times5 = $original * 5;
return round($times5) / 5;
}
So your total is 25, would it be possible to not use floats and use 1->25/25? That way there is less calculations needed... (if any at all)
Why is everyone giving solutions that require a deeper inspection or conversion? Want 0.2? Then:
round($n / 0.2) * 0.2; // $n = 3.78 / 0.2 = 18.9 (=) 19 * 0.2 = 3.8 //
Want 5? Then:
round($n / 5) * 5; // $n = 17 / 5 = 3.4 (=) 3 * 5 = 15 //
It's as simple as that.