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
Related
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
What is the best solution to convert a number to:
one digit after decimal point if that number is a real number
no digits, no decimal point if the number is an integer
Example:
if ($num == 8.2) //display 8.2
if ($num == 8.0) //display 8
Note: I won't have numbers like 8.22 or 8.02. I will have this type of numbers:
1, 1.2, 1.4 ... 2.6, 2.8, 3 ....9.8, 10
If you know for sure that all of your numbers will be in that format, you should be able to just use round. (Normally, round does not work well for formatting, but in this case it should do the job.)
foreach ([8, 8.2, 1, 1.2, 1.4, 2.6, 2.8, 3, 9.8, 10] as $number) {
echo round($number, 1) . PHP_EOL;
}
Some might assume otherwise, but echo round(8.0, 1); displays 8, not 8.0.
if (abs($num - (int)$num) < 0.001)
echo (int)$num;
else
echo number_format($num, 1);
Using floor and some arithmetic and number_format
$num = 8.0;
//8.0 - 8 = 0
//8.2 - 8 = .2
if($num - floor($num)>0) {
// Leaves 1 decimal
echo number_format($num,1);
// or if rounding
//echo round($num, 1);
} else {
// Leaves 0 decimal
echo number_format($num,0);
// or if rounding
//echo round($num, 0);
}
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
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
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.