A parte la funzione " Val( ) " avete un codice più fino del seguente:
Public Sub Main()
Dim s As String
Dim bb As Byte[]
Dim b As Byte
Dim sg As Short = 1
Dim i As Integer
s = " -123456 "
bb = Byte[].FromString(s)
For b = 0 To bb.Max
If Not IsSpace(Chr(bb[b])) Then ' Salta gli eventuali spazi iniziali e finali
If bb[b] = Asc("-") Then sg = -1 ' Verifica se il numero è segnato
If bb[b] < 48 Then Inc b ' Se è segnato, incrementa "b"
i = 10 * i + (bb[b] - Asc("0")) ' Ricava il valore numerico corrispondente al numero stringa
Endif
Next
Print sg * i
End
per convertire un numero segnato di tipo stringa nel corrispondente valore numerico aritmetico (esempio: Intero) ?
Un codice molto simile al precedente:
Public Sub Main()
Dim s As String
Dim bb As Byte[]
Dim b As Byte
Dim sg As Short
Dim i As Integer
s = " 123456789 "
bb = Byte[].FromString(s)
While IsSpace(Chr(bb[b])) ' Salta gli eventuali spazi iniziali
Inc b
Wend
sg = IIf(bb[b] = Asc("-"), -1, 1) ' Verifica se il numero è segnato negativo
If bb[b] < 48 Then Inc b ' Se è comunque segnato ("+" "-"), incrementa "b"
While IsNumber(Chr(bb[b]))
i = 10 * i + (bb[b] - Asc("0")) ' Ricava il valore numerico corrispondente al numero stringa
Inc b
If b = bb.Count Then Break
Wend
Print sg * i
End
E così?
Public Sub Main()
Dim s As String = " -123456 "
Dim n As Float
s = Trim(s)
If IsNumber(s) Then n = CFloat(s)
Print n
End
Volendo credo si possa usare anche il casting o per meglio dire la conversione implicita anche se penso che non sia raccomandabile. Ho cercato in merito ma non ho trovato nulla :-\
Public Sub Main()
Dim s As String = " -123456 "
Dim n As Byte = 6
s = Trim(s)
If IsNumber(s) Then Print s + n
End
per togliere TUTTI gli spazi:
valore=replace(valore," ","")
per convertirlo in numero (float?)
try numeroavirgolamobile=cfloat(valore)
if error then
message.warning ....
end if