Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop decimal place without rounding value

Tags:

php

rounding

I have a decimal value of length 15,4 in my database.

I have a number -23.425 I am trying to round down to -23.42

I have tried the following ways but they all seem to round up to -23.43:

 sprintf("%.2f", $discountQueryResult['value'])

 floor($discountQueryResult['value']*100)/100 

Is there any other way to drop the 3rd decimal place?

like image 447
Sackling Avatar asked Jun 15 '26 20:06

Sackling


1 Answers

You're trying to round a negative number. Rounding down a negative number with floor() will increase its absolute value; remember, -23.43(0) is less than -23.425!

Instead, you're looking to round up its value with ceil():

echo ceil(-23.425 * 100) / 100; // -23.42

This can be seen working here.

like image 154
Obsidian Age Avatar answered Jun 17 '26 10:06

Obsidian Age



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!