Differenze tra le versioni di "Convertire un valore numerico di tipo Float in una stringa di caratteri ASCII"
Da Gambas-it.org - Wikipedia.
(Una versione intermedia di uno stesso utente non è mostrata) | |||
Riga 2: | Riga 2: | ||
− | ==Uso della funzione | + | ==Uso della funzione "CStr()"== |
La modalità più semplice è quella di usare l'apposita funzione ''CStr( )'', che ha la seguente sintassi: | La modalità più semplice è quella di usare l'apposita funzione ''CStr( )'', che ha la seguente sintassi: | ||
Stringa = CStr ( Expression AS Variant ) AS String | Stringa = CStr ( Expression AS Variant ) AS String | ||
Riga 8: | Riga 8: | ||
Mostriamo un esempio pratico: | Mostriamo un esempio pratico: | ||
− | + | Public Sub Main() | |
− | + | Dim f As Float | |
− | + | Dim s As String | |
<FONT Color=gray>' ''Il valore di tipo "Float" da convertire in valore stringa:''</font> | <FONT Color=gray>' ''Il valore di tipo "Float" da convertire in valore stringa:''</font> | ||
f = 123.456 | f = 123.456 | ||
− | s = <FONT Color= | + | s = <FONT Color=red>CStr</font>(f) |
<FONT Color=gray>' ''Mostra la stringa ottenuta:''</font> | <FONT Color=gray>' ''Mostra la stringa ottenuta:''</font> | ||
Riga 24: | Riga 24: | ||
Print Len(s) | Print Len(s) | ||
− | + | End | |
− | |||
Riga 33: | Riga 32: | ||
− | + | Public Sub Main() | |
− | + | Dim bb As New Byte[] | |
− | + | Dim f As Float | |
− | + | Dim s As String | |
<FONT Color=gray>' ''Il valore di tipo "Float" da convertire in valore stringa:''</font> | <FONT Color=gray>' ''Il valore di tipo "Float" da convertire in valore stringa:''</font> | ||
Riga 47: | Riga 46: | ||
Print "Lunghezza della stringa ritornata: "; Len(s); " caratteri" | Print "Lunghezza della stringa ritornata: "; Len(s); " caratteri" | ||
− | + | End | |
− | + | Private Function Float_String(s As Byte[], n As Float) As String | |
− | + | Dim digit, m, m1, neg, usaExp As Integer | |
− | + | Dim wgt As Float | |
If IsNan(n) Then | If IsNan(n) Then | ||
Riga 113: | Riga 112: | ||
Return s.ToString(0, s.Count) | Return s.ToString(0, s.Count) | ||
− | + | End | |
− | + | ==Uso della funzione esterna "sprintf()"== | |
− | ==Uso della funzione esterna | + | Questa modalità prevede l'uso della funzione esterna "sprintf()", dichiarata nel file header ''/usr/include/stdio.h'' . |
− | Questa modalità prevede l'uso della funzione esterna | ||
Al riguardo si rimanda a [[Sprintf_()|questa apposita pagina della Wiki]]. | Al riguardo si rimanda a [[Sprintf_()|questa apposita pagina della Wiki]]. | ||
− | |||
− | |||
− | |||
− | |||
− |
Versione attuale delle 16:38, 1 lug 2024
Per convertire un valore numerico di tipo Float in una stringa di caratteri ASCII è possibile adottare varie modalità.
Uso della funzione "CStr()"
La modalità più semplice è quella di usare l'apposita funzione CStr( ), che ha la seguente sintassi:
Stringa = CStr ( Expression AS Variant ) AS String
Mostriamo un esempio pratico:
Public Sub Main() Dim f As Float Dim s As String ' Il valore di tipo "Float" da convertire in valore stringa: f = 123.456 s = CStr(f) ' Mostra la stringa ottenuta: Print s ' Per verifica mostra la lunghezza della stringa ottenuta: Print Len(s) End
Uso di apposito algoritmo
La seguente modalità utilizza un apposto codice:
Private Const PRECISIONE As Float = 0.00000000000001 Public Sub Main() Dim bb As New Byte[] Dim f As Float Dim s As String ' Il valore di tipo "Float" da convertire in valore stringa: f = 1234567890.12345 s = Float_String(bb, f) Print "Stringa ritornata: "; s Print "Lunghezza della stringa ritornata: "; Len(s); " caratteri" End Private Function Float_String(s As Byte[], n As Float) As String Dim digit, m, m1, neg, usaExp As Integer Dim wgt As Float If IsNan(n) Then s.FromString("nan") Else If IsInf(n) Then s.FromString("inf") Else If n = 0.00 Then s.Push(CByte(Asc("0"))) Else neg = n < 0 If neg Then n = - n ' Calcola l'ordine di grandezza: m = Log10(n) usaExp = (m >= 14 Or (neg And m >= 9) Or m <= -9) If neg Then s.Push(CByte(Asc("-"))) ' Impostazione per la notazione scientifica: If usaExp Then If (m < 0) Then m -= 1.0 n = n / (10.0 ^ m) m1 = m m = 0 Endif If m < 1.0 Then m = 0 ' Converte il numero: While (n > PRECISIONE) Or (m >= 0) wgt = 10.0 ^ m If (wgt > 0) And (Not IsInf(wgt)) Then digit = Floor(n / wgt) n -= (digit * wgt) s.Push(CByte(Asc("0") + digit)) Endif If (m = 0) And (n > 0) Then s.Push(CByte(Asc("."))) Dec m Wend If usaExp Then ' Converte l'esponente: s.Push(CByte(Asc("e"))) If m1 > 0 Then s.Push(CByte(Asc("+"))) Else s.Push(CByte(Asc("-"))) m1 = - m1 Endif m = 0 While m1 > 0 s.Push(CByte(Asc("e") + (m1 Mod 10))) m1 /= 10 Inc m Wend Endif Endif Return s.ToString(0, s.Count) End
Uso della funzione esterna "sprintf()"
Questa modalità prevede l'uso della funzione esterna "sprintf()", dichiarata nel file header /usr/include/stdio.h .
Al riguardo si rimanda a questa apposita pagina della Wiki.