I want to convert float value (Eg:1.0000124668092E+14) to Integer in php,what is the best method for this in php.output should be "100001246680920"
This question is related to
php
floating-point
int
Use round()
$float_val = 4.5;
echo round($float_val);
You can also set param for precision and rounding mode, for more info
Update (According to your updated question):
$float_val = 1.0000124668092E+14;
printf('%.0f', $float_val / 1E+14); //Output Rounds Of To 1000012466809201
What do you mean by converting?
(int) $float
or intval($float)
floor($float)
(down) or ceil($float)
(up)round($float)
- has additional modes, see PHP_ROUND_HALF_...
constants*: casting has some chance, that float values cannot be represented in int (too big, or too small), f.ex. in your case.
PHP_INT_MAX
: The largest integer supported in this build of PHP. Usually int(2147483647).
But, you could use the BCMath, or the GMP extensions for handling these large numbers. (Both are boundled, you only need to enable these extensions)
There is always intval() - Not sure if this is what you were looking for...
example: -
$floatValue = 4.5;
echo intval($floatValue); // Returns 4
It won't round off the value to an integer, but will strip out the decimal and trailing digits, and return the integer before the decimal.
Here is some documentation for this: - http://php.net/manual/en/function.intval.php
I just want to WARN you about:
>>> (int) (290.15 * 100);
=> 29014
>>> (int) round((290.15 * 100), 0);
=> 29015
Use round
, floor
or ceil
methods to round it to the closest integer, along with intval()
which is limited.
http://php.net/manual/en/function.round.php
Source: Stackoverflow.com