Android Question X MOD Y

RichardN

Well-Known Member
Licensed User
Longtime User
B4X:
Sub MinutesToTime(mins As Int) As String

    'Converts integer minutes to a time string 'HH:MM'

    Dim H As Int
    Dim M As Int

    H = mins/60
    M = mins Mod 60
    Log(M)                             'apparently 800 MOD 60 = 19 not 20 ???
    
    Return NumberFormat(H,2,0) & ":" & NumberFormat(M,2,0)

End Sub

This function appears to work 99% of the time. However when mins = 800 the function returns '13:19' instead of '13:20'...........

Why?
 

OliverA

Expert
Licensed User
Longtime User
No issues here:

B4A: 7.80
JDK: 1.8.0_131 (64 bit)
android.jar: android-26
Phone: Original Moto-X w/Android 5.1

B4X:
Dim mins As Int = 800
Dim H As Int
Dim M As Int

H = mins/60
M = mins Mod 60
Log(M)

Log(NumberFormat(H,2,0) & ":" & NumberFormat(M,2,0))
Log(MinutesToTime(800))
Dim test As Float = 800
Log(MinutesToTime(test))
Dim dTest As Double = 800
Log(MinutesToTime(dTest))

Output:
Logger connected to: motorola XT1058
--------- beginning of main
** Service (starter) Destroy **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
20
13:20
20
13:20
20
13:20
20
13:20
** Activity (main) Resume **
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
800/60 is 13,333 so you need float not int
@RichardN has no issues with the hour portion. His issue seems to be the MOD function that should give him the remainder of 20, but in his test case gives him 19.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@OliverA i looked at the wrong part.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Klaus.... Within the Sub H= mins/60 (correctly rounds towards zero) = 13.
M= mins Mod 60. M=19 (incorrect)

However..... If I paste almost identical code elsewhere into the end of Activity_Create() the result quite different and correct.
B4X:
    Dim H As Int
    Dim M As Int
   
    Dim mins As Int = 800
    H = mins/60
    M = mins Mod 60                            'coreectly equates to M=20
   
    Log(NumberFormat(H,2,0) & ":" & NumberFormat(M,2,0))           
                            
                                                       'produces '13:20'

For some reason the problem lies with M = mins Mod 60 which functions abnormally within a Sub but correctly outside!

The investigation continues......
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
this Log(M) gave you also 19?

what is your B4A/JDK/Android SDK Version?

or maybe your problem is NumberFormat , the number input there is double ..
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Well, I tried once more.
I copied your subroutine from post #1 in one of my programs and used Log(MinutesToTime(800)) in different routines, I used also only the code lines in another routine.
And I get allways the same correct result!
Can you make a small project showing the problem, so we could test it.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
There is some other interaction going on here.

I just put the code into a virgin test project and it works perfectly both inside & outside a Sub. There is something going on in that particular situation which is not at all evident. I will do a bit more testing and get back.
 
Upvote 0

Ormente

Member
Licensed User
Longtime User
Is the argument you feed to the Sub a true, explicit int, or is there some kind of conversion (from string, float...) ?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
there some kind of conversion (from string, float...)
For float and double see post #3. I also tried String, but it too produced a correct result for me (and the configuration posted).
 
Upvote 0

Ormente

Member
Licensed User
Longtime User
Yes @OliverA I saw that, and that's because I cant see how 800 mod 60 can be anything other than 20 I'm questioning the value @RichardN gives to his sub.
Anything that result in 800 seems to work, so maybe the value given don't result in 800 when casted to an Int.

Maybe it could be interesting to add log(argVar) just before calling MinutesToTime(argVar), and log(mins) as a first line int the sub.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Thanks for the suggestions guys, I had already been down all those roads.

I spent a lot of time discounting one element at a time to try and solve this issue in code without success - until I noticed some other misbehaviour in the IDE. I transferred the project files from my desktop onto my laptop and found it runs perfectly every time so I must presume some installation corruption issue. I have reinstalled B4A and all the Java dependencies and it now works just fine.

Sometimes the answer is not so simple!
 
Upvote 0
Top