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:
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
I would have used something like for comparisons:
B4X:
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:
B4X:
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.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…