Android Question float decimal point

Mahares

Expert
Licensed User
Longtime User
B4X:
Dim N1 As Float =345.34567
    Dim s As String = NumberFormat(N1, 1, 2)
    Log(s)  '345.35
    
    Dim s As String =NumberFormat2(N1,1,2,2,False)
    Log(s)  '345.35
    
    Dim s As String = NumberFormat(Ceil(N1*100)/100, 1, 2)
    Log(s)  '345.35
    
    Dim s As String = NumberFormat(Floor(N1*100)/100, 1, 2)
    Log(s)    '345.34  ' here is the answer the OP wants
 
Upvote 0

Xfood

Expert
Licensed User
try something like this
Log(InteroDecimale("345.34567",".",2)) ' 345.34
B4X:
Sub InteroDecimale(Numero As String,Separatore As String,Decimali As Int) As String
        'numero = 12345.6789
    Dim FormattoNUmero As List =Regex.Split("\" & Separatore, Numero)
    Dim ritorno As String
       ritorno=FormattoNUmero.Get(0)
    
    If Decimali > 0 Then
        ritorno = ritorno & Separatore & Left(FormattoNUmero.Get(1),Decimali)
    End If
  Return ritorno
End Sub

Sub Left(Text As String, Length As Int)As String
    If Length>Text.Length Then Length=Text.Length
    Return Text.SubString2(0, Length)
End Sub
 
Upvote 0
Top