Android Question How to check for a certain digit

apty

Active Member
Licensed User
Longtime User
I have an app where the user enters valueA e.g 21.20 and also enters an interval value e.g 2.00. The interval value is added to valueA .e.g
if the user enters 21.20 as valueA and 2.00 as interval, then valueB will be 23.20 (i.e. 21.20+2.00).
I would like to make sure valueB doesn't contain a decimal greater than 60 e.g if a user enters valueA as 21.20 and interval as 0.42, valueB becomes 21.62. Instead of this, i want the value to be 22.02 (These values are time values converted to decimal, hence i can't have 21.62)
Please help, how can i ensure valueB moves to next value whenver the decimal is greater than .60?

I don't want to convert the values back to time ( from decimal) as this will make me rewrite the entire app. Kindly assist
 

stevel05

Expert
Licensed User
Longtime User
Assuming that the neither value can be negative, this should work. I am not good with the effects of floating point roundings, so I prefer to do this type of this with Ints where possible.

I am sure there is a more elegant solution.

B4X:
Sub AddTimes(Val1 As Double,Val2 As Double) As Double
    'Return an obviously silly value if either value is < 0
    If Val1 < 0 OR Val2 < 0 Then Return -1
    'Remove the decimals
    Dim Integers As Int =Floor(Val1) +  Floor(Val2)
    Dim Decimals As Int = Round(((Val1 - Floor(Val1)) + (Val2 - Floor(Val2))) * 100)
'    Log("I1 " & Integers)
'    Log("D1 " & Decimals)
    'Add whole minutes represented in the decimals to the integer value
    Integers = Integers + Decimals / 60
    'Remove whole minutes from the Decimal value
    Decimals = Decimals Mod 60
'    Log("I2 " & Integers)
'    Log("D2 " & Decimals)
    Return Integers + (Decimals / 100)
End Sub
 
Upvote 0
Top