How do I reset the third decimal place in php?

Asked

Viewed 1,186 times

2

I need a help, I have following problem, I need to round up the third decimal place, as I do?

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 someone help me?

1 answer

8


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

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 the style of ceil but with decimal places you can do so:

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

echo ceil_dec(3.43, 1); // 3.5
  • But it doesn’t round 3.49 to 3.50 as request, right?

  • But I need to round up as it does?

  • @Tutijapawada if you need to always round up you should use the ceil, not the round.

  • @Tutijapawada think you can: multiply by 100, use the function Ceil and split by 100 again.

  • @Sergio I think you need something else, the ceil round a fraction to the next integer.

  • 1

    @Andersoncarloswoss round(3.49, 1); gives 3.5 but I see the AP always wants to round up.

  • 2

    @Tutijapawada you always want to round up with 1 decimal place, that’s it?

  • 1

    Yes, always up, Ceil is whole and round doesn’t work either ;/

  • @Tutijapawada added another variant, take a look.

  • 1

    @Sergio, perfect now worked thanks! D

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.