Round off decimal values using PHP

PHP => There are three functions available toround off a decimal value
1) Exact round off with and without precision ( if x > 0.5 then taken as x=1 and x < 0.5 then taken as x=0 )
2) Rounded up to nearest integer ( if x > 0 then taken as x=1 and x < 0 then taken as x=0 )
3) Rounded down to nearest integer ( if x > 1 then taken as x=1 and x < 1 then taken as x=0 )

1) syntax: round(number,precision,mode);
Example:(1) (without precision)

 

Example:(2) (with precision)

 

Example:(3) (with mode properties)

 

Mode properties:
a) 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
b) PHP_ROUND_HALF_DOWN -> Round number down to precision decimal places, when it is half way there. Rounds 1.5 to 1 and -1.5 to -1
c) PHP_ROUND_HALF_EVEN -> Round number to precision decimal places towards the next even value
d) PHP_ROUND_HALF_ODD -> Round number to precision decimal places towards the next odd value

2) syntax: ceil(number)
Example:

3) syntax: floor(number);
Example:

 

Leave a Reply

%d bloggers like this: