The content of this section:
PHP, JS small number Rounding Method
The first part:
1, discard the fractional part, and keep the whole number of parts
Copy Code code example:
Php:intval (7/2) js:parseint (7/2)
2, rounding up, with decimals on the integer part plus 1
Copy Code code example:
Php:ceil (7/2) Js:Math.ceil (7/2)
3, rounded.
Copy Code code example:
Php:round (7/2) js:Math.round (7/2)
4, rounding down
Copy Code code example:
Php:floor (7/2) Js:Math.floor (7/2)
The second part, PHP takes the whole common function
In PHP programming, the function of rounding is often used ceil, floor, round, intval.
Let's introduce them separately.
1,ceil--in-process rounding
Introduction description
float ceil (float value)
Returns not less than =noted id=note_temp>value the next integer, value if there is a fractional part to carry, ceil () return type is still float,float value range is usually larger than Eger.
Example 1. Ceil
Copy Code code example:
<?phpecho ceil (4.3); 5echo ceil (9.999); 10?>
2,floor--The rounding-off method
Introduction description
Float floor (float value)
Returns less than value the next integer takes the value fractional part to take the whole floor return type is still float float value range is usually larger than Eger
Example 1. Floor
Copy Code code example:
<?phpecho floor (4.3); 4echo floor (9.999); 9?>
3,round--4 5 in floating point
Introduction description
Float round (float Val [, Precision])
Returns Val Precision (number of digits after decimal decimal point) for 4 5 into the result precision can also be negative or 0 (default)
Example 1. Round
Copy Code code example:
<?phpecho round (3.4); 3echo round (3.5); 4echo round (3.6);//4echo round (3.6, 0);//4//Www.jbxue.comecho round (1.95583, 2); 1.96echo round (1241757,-3); 1242000echo round (5.045, 2); 5.05echo round (5.055, 2); 5.06?>
4,val---to integer patterns of variables
Example 1,val
Copy Code code example:
<?phpecho val (4.3); 4echo Val (4.6); 4 ?>