Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all decimals in PHP

Tags:

database

php

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

like image 894
Thew Avatar asked Feb 04 '11 21:02

Thew


People also ask

How to remove decimal amount in php?

Rather than rounding, how would I remove the decimal notation from this number? floor(17.672); floor() makes it stay as a float, so mark's solution below (same as intval(17.672) ) might be better. echo round(10.5); // Round the number, this example would echo 11.

How do you round off decimals in PHP?

The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a number DOWN to the nearest integer, look at the floor() function.

How do I remove decimal points from a string?

String truncated = String. valueOf((int) doubleValue); We can confidently use this approach when we're guaranteed that the double value is within the range of an int.

How remove extra zeros from decimal in PHP?

$num + 0 does the trick.


2 Answers

You can do it in PHP:

round($val, 0); 

or in your MYSQL statement:

select round(foo_value, 0) value from foo 
like image 146
Yoram de Langen Avatar answered Oct 05 '22 07:10

Yoram de Langen


You can do a simply cast to int.

$var = 252.587254564; $var = (int)$var; // 252 
like image 43
Murilo Vasconcelos Avatar answered Oct 05 '22 08:10

Murilo Vasconcelos