iOS Question What is "Round2" B4A function in B4i?

klaus

Expert
Licensed 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
 
Upvote 0

klaus

Expert
Licensed 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.
 
Upvote 0

roumei

Active Member
Licensed User
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.
 
Upvote 0
Top