Android Question Rounding down

nibbo

Active Member
Licensed User
Longtime User
Simple I am sure but I can't find it in the documentation.
How do I round down to 2 decimal places?
 

wonder

Expert
Licensed User
Longtime User
use floor()
Floor indeed, but it truncates the decimal places. Here's the code snippet that you need:

B4X:
Sub RoundDown(number As Double, decimalPlaces As Int) As Double  
    Return Floor(number * Power(10, decimalPlaces)) / Power(10, decimalPlaces)
End Sub
Corrected version, thanks Sorex.

B4X:
Log(RoundDown(24.1499999)) 'Output: 24.14
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
B4X:
Dim i As Double
i=2.34999
Log( Floor(i*100)/100 )

not sure if it's safe for all values tho :)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
by the way do you sometimes need to look for your cursor with that theme? I use it too at home and sometimes the cursor is kind of lost in the background.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
B4X:
Dim i As Double
i=2.34999
Log( Floor(i*100)/100 )

not sure if it's safe for all values tho :)
Ahaha good job!! I think I like math a little bit too much! ;) You solution is way simpler than mine!

EDIT: No... No issues with the cursor.
 
Last edited:
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Thanks all, yes I found Floor truncates the decimals and returns an integer even though the when you type Floor it implies it will return a double.
As stated above Round2 always rounds up.
I will write a little function using the Floor(double * 100) / 100 which seems the neatest.
Thanks all.
 
Upvote 0
Top