B4R Question How to change the format of two numbers using NumberFormat

Cesar_Morisco

Active Member
Hey guys
All good
How do I format the voltage using the format NumberFormat(voltage,1,2) for example 1.2 v,2,0 etc. Having a comma between the first number places
Thank you in advance!
B4R:
Log("Tensão de entrada: ",NumberFormat(voltagem,1,2), " V" )
Out log Tensão de entrada: 38.56 V
 

hatzisn

Expert
Licensed User
Longtime User
You can do it with this code:

B4X:
Sub ChangeVoltage
    Dim sString As String = "38.56V"
    Log(sString)
    Replace(sString)
    Log(sString) 
End Sub

Private Sub Replace(sVoltage() As Byte)
    Private bc As ByteConverter
    Dim iPos As Int = bc.IndexOf(sVoltage, ".".GetBytes)
    sVoltage(iPos) = ",".GetBytes(0)
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another option:
B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    PrintTemperature("Tensão de entrada: ", 1.2)
    PrintTemperature("Tensão de entrada: ", -1.2)
    PrintTemperature("Tensão de entrada: ", -1)
    PrintTemperature("Tensão de entrada: ", 0)
End Sub

Private Sub PrintTemperature (prefix() As Byte, temp As Float)
    Dim s() As Byte = NumberFormat(temp, 1, 2)
    Dim bc As ByteConverter
    Dim i As Int = bc.IndexOf(s, ".")
    If i > -1 Then
        Log(prefix, bc.SubString2(s, 0, i), ",", bc.SubString(s, i + 1))
    Else
        Log(prefix, s)
    End If
End Sub
 
Upvote 0

Cesar_Morisco

Active Member
You can do it with this code:

B4X:
Sub ChangeVoltage
    Dim sString As String = "38.56V"
    Log(sString)
    Replace(sString)
    Log(sString)
End Sub

Private Sub Replace(sVoltage() As Byte)
    Private bc As ByteConverter
    Dim iPos As Int = bc.IndexOf(sVoltage, ".".GetBytes)
    sVoltage(iPos) = ",".GetBytes(0)
End Sub
Thanks for the answer
I had already resolved it.
I did it this way and had a very satisfactory result but it works.
If I'm wrong, please correct me, I really appreciate it
B4R:
Dim reading As UInt = AdcPin1.AnalogRead
    ' Multiplica para compensar o divisor de tensão 100k/10k
    Dim voltagem As Float = (reading / 1023.0) * 15.0 * 2.8 ' Multiplicando por 2.5 para compensar o divisor de tensão
    Dim TensaoEntrada As Float = voltagem
    Dim TensaoConvertida As Float = TensaoEntrada / 10
    Dim TensaoFormatada As String = NumberFormat(TensaoConvertida, 1, 1)
    Log("Tensão de entrada: ",TensaoFormatada, " V" ) Log  Tensão de entrada: 4.0 V
 
Last edited:
Upvote 0
Top