This question already has an answer here:
Typecasting vs function to convert variable type in PHP
(1 answer)
Closed 6 years ago.
I've got some strings that I need to cast as floats but have seen two apparently different ways of doing it:
$float = floatval ($float);
and
$float = (float) $float;
Are there any differences between the two methods?
In all other cases it will be evaluated as a float. In other words, the $string is first interpreted as INT, which cause overflow (The $string value 2968789218 exceeds the maximum value ( PHP_INT_MAX ) of 32-bit PHP, which is 2147483647.), then evaluated to float by (float) or floatval()
Please have look at PHP Convert String into Float/Double
The best answer for this is Typecasting vs function to convert variable type in PHP
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:
Compare floats in php
(17 answers)
Closed 7 years ago.
I was working on a code and I could not understand the weird result I was getting.
<?php
$a = 0.01;
$p = pow(0.1, 2); // result: 0.01
if( $a < $p ){
echo "true";
}
?>
The result of this condition is always "true" while both of the variables have same value, but the result coming from pow is changing something internally. Seems like I would not be able to rely on this function. Would someone please help me figuring this out ?
its because of float inaccuracy,
take a look at answered question mentioned in comment by b0s3
Read the red warning first
http://www.php.net/manual/en/language.types.float.php. You must never
compare floats for equality. You should use the epsilon technique.
For example:
if (abs($a-$b) < EPSILON) { … } where EPSILON is constant representing
a very small number (you have to define it)
https://stackoverflow.com/a/3149007/4998045
so you can trust pow function but you cant trust float comparsion
PHP Docs said:
base raised to the power of exp. If both arguments are non-negative integers and the result can be represented as an integer, the result will be returned with integer type, otherwise it will be returned as a float.
Maybe you need to convert all to int or all to float.
if( (float)$a < (float)$p ){
echo "true";
}
See it run:
http://phpfiddle.org/main/code/2hv5-n2fw
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;
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Fastest way to convert string to integer in PHP
PHP: Is there any particular difference between intval and (int)?
Is (int)$var same as intval($var) ?
Apparently they both do the same thing.
Is there any situation in which they would return different results?
(int) would seem to be a bit faster than intval, as you don't have the overhead of a function call. intval also allows you to set an optional base to convert to, which might be useful:
int intval ( mixed $var [, int $base = 10 ] )
This question already has answers here:
Is there any particular difference between intval and casting to int - `(int) X`?
(7 answers)
Closed 8 years ago.
What is the better option:
// Option 1:
$intValue = (int) $numericValue;
// Option 2:
$intValue = intval($numericValue);
Is there any difference between these two lines, and which should be used in which situation?
intval is slower than casting to int (it's a function)
intval can accept a base if its first parameter is a string
Since the second item is really obscure, objectively we should say that intval is simply "worse".
However, personally I like reading intval($var) better than (int)$var (the latter may also require wrapping in additional parens) and since if you are converting to int inside a loop you are definitely doing something wrong, I use intval.
All behaviour explained here along with GOTCHAS...
http://php.net/manual/en/function.intval.php
http://www.php.net/manual/en/language.types.type-juggling.php