iOS QuestionWhat is "Round2" B4A function in B4i?

PrayudiPramono

Member
With B4A
a = Round2(10/3 , 2)
a = 3.33

How do I do that with B4i?

klaus

Expert
Longtime User
I use this workaround:
B4X:
``````#If B4i
Private Sub Round2(Number As Double, DecimalPlaces As Int) As Double
Private shift As Double = Power(10, DecimalPlaces)
Return Round(Number * shift) / shift
End Sub
#End If``````

Erel

B4X founder
Staff member
Longtime User
I've yet to see a case where it wasn't a mistake to use Round2.
If you have a number and you want to display it (convert to string) then you should use NumberFormat or B4XFormatter.

klaus

Expert
Longtime User
I use Round2 in the xChart class to calculate scale values by compairing values rounded to 12 decimals instead of the standard 15.
For display I use always NumberFormat.

Erel

B4X founder
Staff member
Longtime User
I would have used something like:
B4X:
``If Abs(v1 - v2) < EPSILON Then ...``

Round2 is also fine for this.

roumei

Active Member
I've yet to see a case where it wasn't a mistake to use Round2.
I've got a (admittedly very rare) case where using Round2 or something similar is quite useful:
A point-in-triangle test can only test if the point is really inside the triangle but fails if it lies on one of the edges. In my case, the three points of a triangle are always on some kind of a grid with equidistant spacing like 0.25 m.
Round2 helps to shift the test point very slightly (only if needed) so that the point-in-triangle test will in most cases be able to 'hit' the inside of a triangle. This approach is much more efficient than additionally testing if the point lies on an edge.

Replies
7
Views
2K
Replies
3
Views
1K
iOS Question Round2 in B4i
Replies
4
Views
1K
iOS Question Round2 equivalent
Replies
1
Views
966
iOS Question round2
Replies
2
Views
1K