33

get this from my database:

252.587254564

Well i wanna remove the .587254564 and keep the 252, how can i do that?

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

Greetings

12 Answers 12

53

You can do it in PHP:

round($val, 0);

or in your MYSQL statement:

select round(foo_value, 0) value from foo
3
  • 8
    I don't think this is a correct answer. The asker is not looking to round up. The asker has 252.587254564 and wants to remove the decimals and be left with 252. round($val, 0) would result in 253 (rounding up). The correct answer to the specific question should not round up, it should either remove the decimal or always rounds down. I would use intval($val) Sep 30, 2020 at 19:38
  • 1
    this rounds, say you have 315.5, it will round to 316.0. not 316. Incorrect answer Nov 16, 2021 at 18:31
  • Well ya, this was answered 10 years ago, when everything was different.. even MySQL's functions output has changed Nov 17, 2021 at 7:58
38

You can do a simply cast to int.

$var = 252.587254564;
$var = (int)$var; // 252
0
12

As Tricker mentioned you can round the value down or you can just cast it to int like so:

$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.
9

In PHP you would use:

$value = floor($value);

floor: Returns the next lowest integer value by rounding the value down if necessary.

If you wanted to round up it would be:

$value = ceil($value);

ceil: Returns the next highest integer value by rounding the value up if necessary.

5

You can just cast it to an int:

$new = (int)$old;
5

Convert the float number to string, and use intval to convert it to integer will give you 1990

intval(("19.90"*100).'')
4

positive number:

round(252.587254564) // 253
floor(252.587254564) // 252
ceil(252.587254564) //252
(int)252.587254564 // 252
intval(252.587254564) // 252
~~252.587254564 // 252

negative number:

round(-252.587254564) // -253
floor(-252.587254564) // -253
ceil(-252.587254564) // -252
(int)-252.587254564 // -252
intval(-252.587254564) // -252
~~-252.587254564 // -252

if you want just remove decimals without round you can use one of above codes except round and floor(for negative number).

But I recommended the last one, it's simpler and faster using prefix ~~

3

Before using above answer what is your exact requirement please see bellow example output.

$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
2

you can use echo (int) 252.587254564;

1

And there is also a not quite advisable method:

strtok($value, ".");

This cuts of the first part until it encounters a dot. The result will be a string, not a PHP integer. While it doesn't affect using the result much, it's not the best option.

0

I see many answers, but the question is:

"Well i wanna remove the .587254564 and keep the 252, how can i do that?"

Since the questioner is asking for php, the function in php will be the one for the job.

$newValue = floor($value);
-1

In MySQL you can use:

select floor(field)

or in PHP you can use:

floor($value);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.