Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert float value to integer in php?

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"

like image 410
sajith Avatar asked May 09 '13 07:05

sajith


People also ask

How do I change float value?

A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function.

How do I echo an integer in PHP?

Answer: Use the strval() Function You can simply use type casting or the strval() function to convert an integer to a string in PHP.

What is Floatval PHP?

The floatval() function is an inbuilt function in PHP which returns the float value of a variable.


2 Answers

What do you mean by converting?

  • casting*: (int) $float or intval($float)
  • truncating: floor($float) (down) or ceil($float) (up)
  • rounding: 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)

like image 144
pozs Avatar answered Oct 16 '22 15:10

pozs


I just want to WARN you about:

>>> (int) (290.15 * 100);
=> 29014
>>> (int) round((290.15 * 100), 0);
=> 29015
like image 33
William Desportes Avatar answered Oct 16 '22 13:10

William Desportes