P PrayudiPramono Member Licensed User Feb 27, 2021 #1 With B4A a = Round2(10/3 , 2) a = 3.33 How do I do that with B4i?
klaus Expert Licensed User Longtime User Feb 27, 2021 #2 I use this workaround: B4X: Copy #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
I use this workaround: B4X: Copy #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
mcqueccu Expert Licensed User Longtime User Feb 27, 2021 #3 Use Numberformat B4X: Copy Log(NumberFormat(10/3,0,2)) Upvote 0
klaus Expert Licensed User Longtime User Feb 27, 2021 #4 It depends on what you want to do. Get a number in return or display the number as a String. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Feb 28, 2021 #5 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. Upvote 0
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 Licensed User Longtime User Feb 28, 2021 #6 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
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 Licensed User Longtime User Feb 28, 2021 #7 I would have used something like: Code: Copy If Abs(v1 - v2) < EPSILON Then ... Round2 is also fine for this. Upvote 0
I would have used something like: Code: Copy If Abs(v1 - v2) < EPSILON Then ... Round2 is also fine for this.
roumei Active Member Licensed User Feb 28, 2021 #8 Erel said: I've yet to see a case where it wasn't a mistake to use Round2. Click to expand... 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
Erel said: I've yet to see a case where it wasn't a mistake to use Round2. Click to expand... 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.