PHP unexpected result of float to int type cast
This is because numbers that have a finite representation in base 10 may or may not have an exact representation in the floating point representation PHP uses.
See
>php -r "echo var_dump(sprintf('%.40F', 39.3 * 100.0));"
string(45) "3929.9999999999995452526491135358810424804688"
Since int
always rounds the number down, a small error in the representation makes the cast round it one number down that you would otherwise expect.
Consider using round
instead.
Surprising float-int conversion in PHP
You usually can not see, if a value is well-convertible into a float just by looking at it. Read The warning in the manual for an example.
Converting number from float to Int returns unexpected answer.
internally php represent $width as float with value: 1.0049999999999999
the casting operation simply strip the number after the coma, the you receive 1004.
Use round instead:
round($width * 1000);
PHP casting float to int returns different value
When you divide $valueAsCents = 54780 / 100 then it becomes a float which is not always accurate in digital form because of the way they are stored. In my tests I got
547.7999999999999545252649113535881042480468750000
When multiplied by 100 this is would be
54779.9999999999927240423858165740966796870000
When PHP casts to int, it always rounds down.
When converting from float to integer, the number will be rounded towards zero.
This is why the int value is 54779
Additionally, the PHP manual for float type also includes a hint that floating point numbers may not do what you expect.
Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....
PHP Type Casting
Was the task to round the float to the nearest whole number? In which case:
$float = 1.92;
$float += 2;
echo round($float);
outputs 4. Or a straight cast:
$float = 1.92;
$float += 2;
echo (int) $float;
outputs 3.
Type-Casting in C from Float to Int results in wildly different number, why?
The maximum value a 4-bytes integer could hold is ranged from -2,147,483,648 to 2,147,483,647. The result of 10^10 is 10,000,000,000, which exceeds the limit and the conversion overflows which gives unexpected results. That's all.
PHP Type Casting to integer
If we will cast float number to integer then output will be the number before decimal. Means if we will cast 10.9
to integer then output will be 10
You should also refer these functions ceil() , floor() and round().
For more detail, refer below link also,
PHP unexpected result of float to int type cast
Hope it will help you :-)
Why is PHP typecasting behaving like this? (int) mis-rounding numbers?
from manual:
When converting from float to integer, the number will be rounded towards zero.
If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms)
, the result is undefined, since the float doesn't have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!
Warning
Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results.
<?php
echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
?>
Read here
Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....
in your case may be 18.99*100=1898.999999999999772626324556767940521240234375
so int
truncated it to 1898.
php float number error
Your code seems to have a hidden character ?
Try copy and use this:
<?php
$_web_lat=18.501059;
$_web_long=73.862686;
echo $_web_lat .'='. $_web_long;
?>
Related Topics
How to Convert PHP Date Formats to Gmt and Vice Versa
PHP Unexpected Result of Float to Int Type Cast
Jquery Ajax Passing Value on PHP Same Page
How to Replace a Function in PHP (Such as Mail) and Make It Do Something Else
Regex Matching Links Without <A> Tag
Header Location Not Working in My PHP Code
Split Alphanumeric String Between Leading Digits and Trailing Letters
Skip First Line of Fgetcsv Method
Wordpress Prepared Statement with In() Condition
Sending Multiple Attachment in an Email Using PHP