Differenze tra le versioni di "Conoscere le impostazioni del separatore decimale nel sistema"
Da Gambas-it.org - Wikipedia.
(4 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 2: | Riga 2: | ||
===1<SUP>a</sup> modalità=== | ===1<SUP>a</sup> modalità=== | ||
− | Mediante l'uso della funzione stringa | + | Mediante l'uso della funzione stringa "Str()", la quale nell'effettuare la conversione utilizza la corrente localizzazione per convertire numeri e date. [<FONT Size=1>[[#Note|nota 1]]</font>] |
Public Sub Main() | Public Sub Main() | ||
− | Dim | + | Dim f as float |
Dim b as string | Dim b as string | ||
− | + | f = 5/2 | |
− | + | s = Str(f) | |
− | Print Mid( | + | Print Mid(s, 2, 1) |
+ | |||
+ | <FONT Color=gray>' ''oppure:''</font> | ||
+ | Print s[1] | ||
+ | <FONT Color=gray>' ''oppure più brevemente:''</font> | ||
+ | Str(f)[1] | ||
End | End | ||
Riga 21: | Riga 26: | ||
Public Sub Main() | Public Sub Main() | ||
− | Dim | + | Dim f As Float |
− | Dim | + | Dim s As String |
Dim p As Pointer | Dim p As Pointer | ||
− | + | f = 5 / 7 | |
− | + | s = Str(f) | |
− | p = | + | p = Alloc(s) |
− | <FONT Color=gray>' '' | + | <FONT Color=gray>' ''Dereferenzia il Puntatore leggendo - come valore Byte - solo il 2° valore della originaria stringa.'' |
− | ' ''Quindi | + | ' ''Quindi converte quel valore di tipo Byte nel corrispettivo carattere ASCII:''</font> |
− | + | Print Chr(Byte@(p + 1)) | |
+ | |||
+ | Free(p) | ||
End | End | ||
Riga 38: | Riga 45: | ||
===3<SUP>a</sup> modalità=== | ===3<SUP>a</sup> modalità=== | ||
− | Mediante un vettore di tipo | + | Mediante un vettore di tipo Byte[] e la funzione "Str()": |
Public Sub Main() | Public Sub Main() | ||
− | Dim | + | Dim f As Float |
Dim bb As Byte[] | Dim bb As Byte[] | ||
− | + | f = 5 / 7 | |
− | bb = Byte[].FromString(Str( | + | bb = Byte[].FromString(Str(f)) |
Print Chr(bb[1]) | Print Chr(bb[1]) |
Versione delle 05:38, 22 dic 2023
Per conoscere quale sia il "separatore decimale" in uso in un sistema, è possibile adottare diverse modalità. Ne mostriamo alcune:
1a modalità
Mediante l'uso della funzione stringa "Str()", la quale nell'effettuare la conversione utilizza la corrente localizzazione per convertire numeri e date. [nota 1]
Public Sub Main() Dim f as float Dim b as string f = 5/2 s = Str(f) Print Mid(s, 2, 1) ' oppure: Print s[1] ' oppure più brevemente: Str(f)[1] End
2a modalità
Con l'uso di una variabile di tipo Puntatore:
Public Sub Main() Dim f As Float Dim s As String Dim p As Pointer f = 5 / 7 s = Str(f) p = Alloc(s) ' Dereferenzia il Puntatore leggendo - come valore Byte - solo il 2° valore della originaria stringa. ' Quindi converte quel valore di tipo Byte nel corrispettivo carattere ASCII: Print Chr(Byte@(p + 1)) Free(p) End
3a modalità
Mediante un vettore di tipo Byte[] e la funzione "Str()":
Public Sub Main() Dim f As Float Dim bb As Byte[] f = 5 / 7 bb = Byte[].FromString(Str(f)) Print Chr(bb[1]) End
4a modalità
Utilizzando un paio di specifiche funzioni esterne della libreria /usr/include/locale.h:
Library "libc:6" Public Struct Lconv decimal_point As Pointer thousands_sep As Pointer End Struct Private Const LC_NUMERIC As Integer = 1 ' char *setlocale (int __category, const char *__locale) ' Set and/or return the current locale. Private Extern setlocale(category As Integer, locale As String) As Pointer ' struct lconv *localeconv(void) ' Return the numeric/monetary information for the current locale. Private Extern localeconv() As Lconv Public Sub Main() Dim lc As New Lconv ' Specifichiamo la categoria e l'ambiente dell'impostazione locale per lingua: setlocale(LC_NUMERIC, System.Language) ' Ritorna un Puntatore alla Struttura che dà informazioni sui simboli numerici secondo l'impostazione locale: lc = localeconv() With lc Print "- Impostazioni locali numeriche -\n\nSeparatore decimale: "; String@(.decimal_point) Print "Separatore migliaia: "; String@(.thousands_sep) End With End
Note
[1] Codice proposto dal membro Akrobaticone del forum gambas-it.org .