Removing all decimals in PHP

get this from my database:

252.587254564

Well, I want to remove .587254564 and save 252 , how can I do this?

What function should I use and can you show me an example?

Hi

+10
source share
10 answers

You can do this in PHP:

 round($val, 0); 

or in the MYSQL statement:

 select round(foo_value, 0) value from foo 
+17
source

You can just cast to int .

 $var = 252.587254564; $var = (int)$var; // 252 
+8
source

In PHP you would use:

 $value = floor($value); 

floor : Returns the next smallest integer value, rounding the value if necessary.

If you would like to round this would be:

 $value = ceil($value); 

ceil : returns the next largest integer value, rounding the value if necessary.

+5
source

As Tricker mentioned, you can round the value down, or you can just pass it to int like this:

 $variable = 252.587254564; // this is of type double $variable = (int)$variable; // this will cast the type from double to int causing it to strip the floating point. 
+4
source

You can just translate it to int :

 $new = (int)$old; 
+4
source

you can use echo (int) 252.587254564;

+2
source

And there is also a not entirely appropriate method:

 strtok($value, "."); 

These are cuts of the first part until she meets a point. The result will be a string, not a PHP integer. Although this does not greatly affect the result, it is not the best option.

+1
source

Before using the above, answer what your exact requirement is, see the example below.

 $val = 252.587254564; echo (int)$val; //252 echo round($val, 0); //253 echo ceil($val); //253 $val = 1234567890123456789.512345; echo (int)$val; //1234567890123456768 echo round($val, 0);//1.2345678901235E+18 echo ceil($val); //1.2345678901235E+18 $val = 123456789012345678912.512345; echo (int)$val; //-5670419503621177344 echo round($val, 0);//1.2345678901235E+20 echo ceil($val); //1.2345678901235E+20 
+1
source

In MySQL you can use:

 select floor(field) 

or in PHP you can use:

 floor($value); 
0
source

Convert a floating point number to a string, and use intval to convert it to an integer, which will give you 1990

 intval(("19.90"*100).'') 
0
source

All Articles