Android Question Maths Problem in B4X

Theera

Well-Known Member
Licensed User
Longtime User
Refer to this ,I'm looking for Maths' Formulas for B4X
Assume In VB : 12 \6 =2
What's that in B4X?
 
Last edited:

Sagenut

Expert
Licensed User
Longtime User
Not sure but it could be
B4X:
Log(12 Mod 6)
Post other calculations example if this is not the answer.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Not sure but it could be
B4X:
Log(12 Mod 6)
Post other calculations example if this is not the answer.
It is an integer division. Like 17\4 = 4
17 mod 4 = 1
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
In this case I think that there is an error in the opening question.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
In this case I think that there is an error in the opening question.
I might be wrong but from memory that is what VB does when using \
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
What about
B4X:
Log((11 / 4).As(Int))
or
B4X:
Dim numb as Int = (11 / 4)
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I think this gives the effect you want
(a - (a mod b)) / b
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
It's called floor division. Here is the tested code:

B4X:
    Dim x As Int
    Dim y As Int = 2
    Dim result As Int

    For x=1 To 20
    result = Floor(x / y) ' Perform division and discard the fractional part
    Log($"${x}->${result}"$) ' Outputs: Result
    Next

Results:
1->0
2->1
3->1
4->2
5->2
6->3
7->3
8->4
9->4
10->5
11->5
12->6
13->6
14->7
15->7
16->8
17->8
18->9
19->9
20->10
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I know this might be a little crazy but when having to do money math and want the remainder I would use longs or ints and multiple the number by 100

Dim Value1 As Int = 1300 ' $13.00
Dim Value2 As Int = 2
Dim Value3 As Int = Value1 / Value2
Dim Value4 As Int = Value3 / 100
Dim Value5 As Int = Value3 - (Value4 * 100)

Log(Value3) ' Amount
Log(Value4) ' Whole Dollars
Log(Value5) 'Cents

I'm sure not a good solution but will give you both answer and remainder in one variable
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I'm sorry that I wrote the incorrect answer.
12\6=2 <---correctly, Get only integer number,(notget decimal number)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…