klaus Expert Licensed User Longtime User Feb 23, 2020 #1 In B4A and B4J we have the Round2(Number As Double, DecimalPlaces As Int) Can you please add in B4i, if Is it possible. Currently I use this workaround: B4X: Copy Private Sub Round2(Number As Double, DecimalPlaces As Int) As Double Private str As String str = NumberFormat(Number, 1, DecimalPlaces) Number = str Return Number End Sub
In B4A and B4J we have the Round2(Number As Double, DecimalPlaces As Int) Can you please add in B4i, if Is it possible. Currently I use this workaround: B4X: Copy Private Sub Round2(Number As Double, DecimalPlaces As Int) As Double Private str As String str = NumberFormat(Number, 1, DecimalPlaces) Number = str Return Number End Sub
Erel B4X founder Staff member Licensed User Longtime User Feb 23, 2020 #2 Why do you need it? In most cases it is a mistake to use Round2.
klaus Expert Licensed User Longtime User Feb 23, 2020 #3 I use it in the xChart class to compare Doubles limited to 14 decimal numbers to calculate logarithmic scales.
I use it in the xChart class to compare Doubles limited to 14 decimal numbers to calculate logarithmic scales.
Erel B4X founder Staff member Licensed User Longtime User Feb 24, 2020 #4 I would have used something like for comparisons: Code: Copy Public epsilon As Double = 0.01 'constant 'compare If Abs(d1 - d2) < epsilon Then 'they are "equal" Much simpler than the implementation of Round2 which is: Code: Copy Dim shift As Double = Power(10, DecimalPlaces) Return Round(Number * shift) / shift It was a mistake to add it to the core library. Its purpose was similar to NumberFormat and NumberFormat is the correct approach for conversation of numbers to strings.
I would have used something like for comparisons: Code: Copy Public epsilon As Double = 0.01 'constant 'compare If Abs(d1 - d2) < epsilon Then 'they are "equal" Much simpler than the implementation of Round2 which is: Code: Copy Dim shift As Double = Power(10, DecimalPlaces) Return Round(Number * shift) / shift It was a mistake to add it to the core library. Its purpose was similar to NumberFormat and NumberFormat is the correct approach for conversation of numbers to strings.