Differenze tra le versioni di "Convertire un valore numerico di tipo Float in una stringa di caratteri ASCII"

Da Gambas-it.org - Wikipedia.
(Creata pagina con "Per convertire un valore numerico di tipo ''Float'' in una stringa di caratteri ASCII è possibile adottare varie modalità. ==Uso della funzione ''CStr( )''== La modalità...")
 
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 28: Riga 28:
  
  
 +
==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
 +
 
 +
<FONT Color=gray>' ''Il valore di tipo "Float" da convertire in valore stringa:''</font>
 +
  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
 +
   
 +
<FONT Color=gray>' ''Calcola l'ordine di grandezza:''</font>
 +
    m = Log10(n)
 +
    usaExp = (m >= 14 Or (neg And m >= 9) Or m <= -9)
 +
    If neg Then s.Push(CByte(Asc("-")))
 +
   
 +
<FONT Color=gray>' ''Impostazione per la notazione scientifica:''</font>
 +
    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
 +
   
 +
<FONT Color=gray>' ''Converte il numero:''</font>
 +
    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
 +
<FONT Color=gray>' ''Converte l'esponente:''</font>
 +
      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'''
  
<FONT Color=Red size=4><B>Pagina in costruzione !</b></font>
+
 
 +
 
 +
==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 [[Sprintf_()|questa apposita pagina della Wiki]].

Versione attuale delle 07:30, 19 ago 2016

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.