32

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

Thew
  • 15,789
  • 18
  • 59
  • 100

12 Answers12

54

You can do it in PHP:

round($val, 0);

or in your MYSQL statement:

select round(foo_value, 0) value from foo
Yoram de Langen
  • 5,391
  • 3
  • 24
  • 31
  • 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) – scmccarthy22 Sep 30 '20 at 19:38
  • 1
    this rounds, say you have 315.5, it will round to 316.0. not 316. Incorrect answer – Tim Bogdanov Nov 16 '21 at 18:31
  • Well ya, this was answered 10 years ago, when everything was different.. even MySQL's functions output has changed – Yoram de Langen Nov 17 '21 at 07:58
32

You can do a simply cast to int.

$var = 252.587254564;
$var = (int)$var; // 252
Murilo Vasconcelos
  • 4,677
  • 24
  • 27
10

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.
ludesign
  • 1,353
  • 7
  • 12
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.

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
JoeyH
  • 335
  • 2
  • 10
5

You can just cast it to an int:

$new = (int)$old;
MarioVW
  • 2,225
  • 3
  • 22
  • 28
4

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

intval(("19.90"*100).'')
Tinh Dang
  • 448
  • 4
  • 11
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
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
2

you can use echo (int) 252.587254564;

KJYe.Name
  • 16,969
  • 5
  • 48
  • 63
2

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 ~~

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
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.

mario
  • 144,265
  • 20
  • 237
  • 291
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);
Brian Fisher
  • 23,519
  • 15
  • 78
  • 82