Wish Add the Round2 method in B4i

klaus

Expert
Licensed User
Longtime User
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
 
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.
 
Back
Top