Android Question Interger Division

pixelpop

Active Member
Licensed User
Longtime User
I have searched the forum and found some discussion on a "missing" math function, specifically the integer divider function. Not having found an implementation of either "\" or "div", what is the proper way to divide two integers and return the correct answer?

If "55 / 10" returns 5 then what function do I use to return 5.5?
 

mangojack

Expert
Licensed User
Longtime User
This works ?
B4X:
Dim  num1 = 55 As Int
Dim  num2 = 10 As Int
 
Label1.Text = num1 / num2  'result = 5.5
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can assign the result of the calculation to a float or a double.
 
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
Not if all three numbers are Dim'ed as Int variables. For instance:

Dim weight As Int
Dim scale_packet(6) As Int
-------
weight = scale_packet(4) / 10

If scale_packet(4) = 55 then weight will return 5, not 5.5.

But if weight is Dim'ed as Double, weight returns the correct 5.5.
 
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
You are right, and that was my error. Based on stevel05's comment, I changed weight from an Int to a Double and all is right with the world. :)
 
Upvote 0
Top