This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 3 years ago.
I'm trying to show one decimal place for rating number I have
but it returns unexpected value.
I used number_format and the round functions and both have the same issue, or i'm doing something wrong.
I tried to make this number show one decimal number
4.96 and it always returns 5 instead of 4.9
number_format(4.96, 1)
round(4.96, 1)
round(4.96, 1,PHP_ROUND_HALF_DOWN)
both functions returns 5 instead of 4.9
I searched all answers but couldn't find anything helpful.
Rounding 4.96 will round .9 up, so it will be 5 in all cases. If you want to do it without rounding, you may have to tweak it a bit to fool it:
floor(4.96 * 10) / 10; // 4.9
Here's a function you can use to achieve this.
function convertToSingleDecimal($num, $precision = 2) {
return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
print convertToSingleDecimal("4.96", 1);
Related
This question already has answers here:
Comparing floats - same number, but does not equal? [duplicate]
(3 answers)
Is floating point math broken?
(31 answers)
Closed 2 months ago.
dump($available_funds);
dump($meal_price);
if ($available_funds < $meal_price) {
dd('hit');
return false;
}
$available_funds and $meal_price are both 'double' values set to 2.78
Why would the if statement be hit when the values are the same?
I have attempted to (float) the variables and floatval() to try and update the types to see if this would resolve the condition but had no luck.
The problem may be due to the precision of the double data type. double values can have up to 15 decimal digits of precision, but in some cases, the actual value stored may not have the same precision as the declared type. This can cause problems when comparing double values, as the values may not be exactly equal even if they appear to be the same.
One solution to this problem is to use the round() function to round the values to a specific number of decimal places before comparing them. For example, you could use the following code to compare the values with two decimal places of precision:
$available_funds = round($available_funds, 2);
$meal_price = round($meal_price, 2);
if ($available_funds < $meal_price) {
dd('hit');
return false;
}
This question already has answers here:
Rounding to nearest fraction (half, quarter, etc.)
(4 answers)
Closed 3 years ago.
I was wondering what php function could help me to do what I am looking for. I want to round number to the half up.
For example:
1.16 => 1.5,
0.5 => 0.5
1 => 1
I wanted to use round function like this :
round(1.16, 0.5, PHP_ROUND_HALF_UP)
But it returns 1 instead of 1.5. I probably dont understand well what the function is doing.
Here is one option. We can multiply the input number by 2, then take the ceiling, and finally divide by two, e.g.
$input = 1.16;
$input_rounded = ceil(2*$input) / 2;
echo $input_rounded;
This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 4 years ago.
I'm probably not looking hard enough but the common question about php rounding is rounding up, not down.
For example I am trying round this..
<?php $roundDown = 768; ?>
Down to..
<?php var_dump($roundDown) /* 700 */ ?>
Whats the simplest method to do this, or is it because the number is closer to 800 that it's not technically rounding?
Whats the function that I need to do this if it's not rounding?
A little point in the right direction would be much appreciated.
You can try ceil() and floor() function.
echo floor(768 / 100) * 100; // Output:700
echo ceil(768 / 100) * 100; // Output:800
This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 6 years ago.
After converting currencies I end up with the numbers below:
3628, 5987 and 2359.
I'd like to round them up so they would appear as 3630, 5990 and 2360.
What is the best approach to accomplish this?
My idea of doing this is adding a 0. in front of the number to achieve 0.3628, then using round(0.3628, 3) so that I would get 0.363 and then finally id have to remove 0. and add another zero at the end to achieve 0.3630.
There must be a better way to do it.
Like so:
echo ceil(5987 / 10) * 10;
outputs 5990
This question already has an answer here:
explain php output
(1 answer)
Closed 9 years ago.
I am trying to understand converting from a float to an int in PHP ,and came across the following code:
echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
My question is why does it echo 7 instead of 8? What is the logic behind it?
Here:
$var = (int) $othervar;
I would guess:
$var = int($othervar);
would work too. BUT IT DOES NOT!
gettype() will tell you the type, get_class() will tell you the class of an object, both return strings, get_class() returns the current class if no argument or null is provided, if you provide a non-object to get_class() it's some sort of error.
These functions are an example of how bad php, gettype() and get_class(), fun fact, because you have to play "guess which has an underscore" you also have is_a() (function, expects $var and "typename") and instanceof (operator).
Instead it is:
$var = intval($othervar);
and there's floatval, the whole lot have val after them! More PHP sillyness.
Note that:
$var = (string) $othervar;
invokes $othervar's __toString() method, but there isn't a __toInt() method, just strings.
Hope this helps.
Addendum:
Integers are always truncated, hence 7.
Other addendum:
If you want more controlled conversion you can use round() which works as one would expect, floor() (rounds down, so 7 less x less 8 floors to 7, -3 less x less-2 floors to -3) and ceil() which rounds up (7 less x less 8 ceils to 8, -3 less x less -2 ceils to -2)
This question is an almost exact copy of:
Why would on earth PHP convert the result of (int) ((0.1+0.7)*10) to 7, not 8?
Even the example!
Because the actual value stored in memory is something like 7.999999999999999999999999 which is caused by rounding error.
you can also use intval()
$float = 7.99999999999999999999999999999999;
$int = intval($float);
echo $int;