PHP Number Tutorials

Number in PHP belongs to one of the following primitive data types: Integer, Floating point number or Float.

Round a number up to nearest 5

$roundedNumber = (ceil($number)%10 === 0) ? ceil($number) : ceil($number/5)*5;
$roundedNumber = (floor($number)%10 === 0) ? floor($number) : floor($number/5)*5;

Round a number up to nearest 10

$roundedNumber = round($number, -1, PHP_ROUND_HALF_UP);
$roundedNumber = round($number, -1, PHP_ROUND_HALF_DOWN);
$roundedNumber = ceil($number / 10) * 10;
$roundedNumber = floor($number / 10) * 10;

Show a number to two decimal places and grouped thousand

$number = "1249865.6555";
echo number_format($number, 2, '.', '');
//output: 1249865.66
echo number_format($number, 2, '.', ',');
//output: 1,249,865.66

Convert String to Integer and Float

$string = '546.2121SomeCharacters';
$int = intval($string);
$float = floatval($string);

$string = "522.34";
$int = (int) $string;
$float = (float) $string;

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close