PHP Round Off Decimal or Convert to Decimal Point
Round Off Decimal
Round a number up or down or Round off a floating decimal point number using PHP’s functions round().
Example. round();
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>
Convert to Decimal Point
Example. number_format();
<?php
$number = “28″;
$number = number_format($number, 2);
echo $number; // equals “28.00″
?>
PHP number formatting for decimal places
// The above line will display 2,512,590 ( without decimal and rounded)
echo “<br> The value after formating = “.number_format($number,3);
// Will display 2,512,589.668 ( with , after each thousand and 3 digits after decimal
// rounded )
echo “<br> The value after formating = “.number_format($number,3,”#”,”:”);
// Will display 2:512:589#668 ( with : after each thousand and # as decimal symbol )
Happy Coding
Filed under: Basic Scripts, Php-MySQL, php | Tagged: decimal, float, math, number_format, php, round
