Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP format a number to have no decimal places?

I have a number (as string) like this: 1.00000

How can I reformat such numbers to only look like 1?

Thanks

like image 207
EOB Avatar asked Apr 26 '12 11:04

EOB


People also ask

How do I restrict decimal places in PHP?

Parameter Values Specifies a constant to specify the rounding mode: PHP_ROUND_HALF_UP - Default. Rounds number up to precision decimal, when it is half way there. Rounds 1.5 to 2 and -1.5 to -2.

How do you round to no decimal places?

Round a number up by using the ROUNDUP function. It works just the same as ROUND, except that it always rounds a number up. For example, if you want to round 3.2 up to zero decimal places: =ROUNDUP(3.2,0) which equals 4.

How do I round a number in PHP?

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


1 Answers

Use number_format()

echo number_format('1.000000'); // prints 1

Or use intval()

echo intval('1.000000'); // prints 1

Or cast it as an integer

echo (int) '1.000000'; // prints 1
like image 174
John Conde Avatar answered Oct 11 '22 09:10

John Conde