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....
How to convert float percent value to integer percent?
<?php number_format(3.8461538461538); ?>
You can specify in second argument the number of decimal points (default is 0).
Convert float number to int or string
I found the problem and the solution reading the comments :)
The problem: I was retrieving data from a webservice via curl and json_encoding the result. At this point, json_encode converts bigints into floats.
I solved it passing JSON_BIGINT_AS_STRING argument to json_encode.
It's documented in PHP documentation:
http://php.net/manual/es/function.json-decode.php
Lost data when convert float to int in php
Problem is when caculate the float, I still dont know why they have different like bellow
$baseprice= 1705000/1.1 ; // 1550000
var_dump( decbin( $baseprice ));
var_dump( decbin(1550000));
We can fix this by
$baseprice = round( $baseprice );
How do I convert a string to a number in PHP?
You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, cast it:
$num = "3.14";
$int = (int)$num;
$float = (float)$num;
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.
how to convert a float value to an integer number in php
Is this what you are after ?
function remdec($num) { return intval(str_replace(".", "", strval($num))); }
$number = remdec(1.5058) - remdec(1.5215);
echo $number;
FIDDLE
PHP - Converting a String float to an Integer - Wrong value
First of all, read Is floating point math broken?
8.54 happens to be one of those numbers which can be written precisely in decimal, but not in binary, so (float)"8.54"
will actually create a number very very close to, but slightly under, 8.54.
Multiplying that by 100 will create a number very very close to, but slightly under, 854. And casting to int
will find the next smallest integer (e.g. 853.9 becomes 853, not 854).
I can think of two solutions:
- After multiplying by 100, round to the nearest whole number, then convert to integer. Unlike 8.54, 854 itself can be represented precisely in floating point, so
round(8.54 * 100)
will give a number that is exactly 854, rather than slightly under it. So(int)round((float)"8.54" * 100)
will give 854 as an integer. - Instead of multiplying by 100, remove the
.
from the string, and convert to int directly, e.g.(int)str_replace(".", "", "8.54"));
How to convert number to float in PHP
You can use number_format function to format the numbers for desired output.
$foo = "105.01";
$zoo = "105";
echo number_format((float)$foo, 2, '.', ''); //Output 105.01
echo number_format((float)$zoo, 2, '.', ''); //Output 105.00
Related Topics
Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted (Tried to Allocate 71 Bytes)
Best Way to Parse an Invalid HTML in PHP
PHP Check If Time Is Between Two Times Regardless of Date
How to Print Multidimensional Arrays in PHP
Move an Array Element to a New Index in PHP
A _Construct on an Eloquent Laravel Model
How to Extract or Uncompress Gzip File Using PHP
How to Iterate Through Dom Elements in PHP
Which Is the Best Character Encoding for Japanese Language for Db, PHP, and HTML Display
Utf-8 Problems While Reading CSV File with Fgetcsv
Time Calculation in PHP (Add 10 Hours)
How to Get the External Ip of My Server Using PHP
How to Serve an Image with Laravel