PHP round number with 2 digits to the nearest float number - php

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

Related

Round values depend rounding value

if use round() , you will round up if number higher than 0.5 and round down if number less than 0.5, i have a condition if you round higher than 0.3 it will round up and less than 0.3 it will round down.
<?php
//normal round
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
//round i want
echo round(3.34); // 4
echo round(3.29); // 3
echo round(3.3); // 3
?>
You can define your own rounding-function, where you get the fraction of the number by subtracting the floored number from itself, then compare that against your limit.
function my_round($value, $round_up_from = 0.3) {
$fraction = $value - floor($value); // Get the fraction
if ($fraction >= $round_up_from) { // If the fraction is equal to or above threshold, ceil it
return ceil($value);
}
return floor($value); // Otherwise, floor it
}
Live demo at https://3v4l.org/Uo0vK

PHP enter percentage number round to 2 decimal places not working

php number round to 2 decimal places not working my number is 0.00000000000000000000001 and I want result as 0.01. I tried php number_format() function php round() function but they didn't work for me.
Just make a simple function that returns either the rounded value or the minimum value you would like...
function round_special($x)
{
if ($x == 0) return 0;
$rounded = round($x, 2);
$minValue = 0.01;
if ($rounded < $minValue) {
return number_format($minValue, 2);
} else {
return number_format($rounded, 2);
}
}
So, results would look like:
$x = 0.00000000000000000000001;
echo round_special($x); // 0.01
echo round_special(0.0001); // 0.01
echo round_special(55); // 55.00
echo round_special(0.6); // 0.06
0.01 is far far greater than 0.00000000000000000000001. You can not round it to 0.01 . 0.006 can be rounded to 0.01 because they are very close to each other.
Number_format() and round() will not work in your case.
This functions adjust the decimals as per original math rules
in your case result will be always 0.00...as .01 will get converts to 0.0

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
}

Rounding to nearest fraction (half, quarter, etc.)

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

Use PHP to round floats that aren't like 1.05

Is it possible to round a number where if it's .5, just leave it, anything below .5 round down, anything above .5 round up?
For example:
5.0 * 1.35 = 6.75 // leave it
5.2 * 1.35 = 7.02 // round down to 7.00
5.5 * 1.35 = 7.56 // round up to 8.00
I've formatted with round($n,0, PHP_ROUND_HALF_UP) where $n is the product from the above calc , which leaves 6.75 but returns 7.02 for the next one. I also tried round($n,-1, PHP_ROUND_HALF_UP) which gives me the 7.00 on the second calc but then of course won't return a 6.75 for the first, instead it returns 680.
This is a ticket markup calculation where the user enters the first number and is multiplied by the second. I actually remove the decimal because they don't want to see it, and they want this sort of customized rounding on the result.
function myround($num, $prec) {
$rhu = round($num, $prec, PHP_ROUND_HALF_UP);
$rhd = round($num, $prec, PHP_ROUND_HALF_DOWN);
return ($rhu + $rhd) / 2;
}
Works for any precision you like. For hundreth's place, as in the example, $prec would need to be 2.
The only way to determine the value of the last non-zero digit of a given floating point number in PHP is to convert it to a string.
$str = (string) $float;
$result = ($str[strlen($str) - 1] == 5) ? $float : round($float);
Example
Of course, no matter what you do it will be subject to a small margin of error because of the floating point precision issue.
$n = round($n, 2);
if($n % .05 != 0 || $n % .1 == 0)
{
$n = round($n);
}
Does this work for you? I'm assuming the 5 you speak of is the hundredth digit, and if it's not 5 then you want a whole number.

Categories