#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
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.
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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.