How do I round the third decimal place in php?

1

I need some help, I have the following problem, I need to round the third decimal place up, how do I do it?

EX:

From 3.49 converts to 3.50 From 3.48 converts to 3.50 From 3.43 converts to 3.50 From 3.42 converts to 3.50

When I use the function round it converts to 4.00

Can anyone help me?

    
asked by anonymous 06.07.2017 / 16:47

1 answer

6

You have to use the second argument of this function round which is exactly the number of decimal places, the precision.

Example:

echo round(3.425);     // 3
echo round(3.425 , 1); // 3.4
echo round(3.425 , 2); // 3.43
echo round(3.425 , 3); // 3.425

To convert to ceil style but with decimal places you can do this:

function ceil_dec($val, $dec) { 
    $pow = pow(10, $dec); 
    return ceil($pow * $val) / $pow; 
} 

echo ceil_dec(3.43, 1); // 3.5
    
06.07.2017 / 16:50