PDA

Visualizza versione completa : VB e separatori decimali


NS-1
25-06-2004, 10.10.09
Ciao,

nel programma che sto facendo acquisisco dei dati numerici con la virgola... es. 545,23

senza utilissare replace vari, come posso sostituire la virgola con il punto? es. 545,23 ---> 545.23


grazie

Ns-1

LoryOne
25-06-2004, 10.54.15
E' il valore di tutto ciò he precede la virgola & "." & tutto ciò che viene dopo

Es:
a$="534,32"
Print Val(Left$(a$, InStr(a$, ",") - 1) & "." & Mid$(a$, InStr(a$, ",") + 1))

LoryOne
25-06-2004, 11.15.30
Qualcosa di più elegante:
a$ = "12350,03"
a = Format$(a$, "##,##0.00")

P8257 WebMaster
25-06-2004, 11.39.29
Una domanda x Lory:
non c'é la possibilità di settare nel codice le impostazioni internazionali? .. come separatore decimale, numeri dopo la virgola, valuta ecc. ecc. praticamente l'equivalente di "setlocale" in C?

Bye :cool:

LoryOne
25-06-2004, 12.20.10
Domanda arguta ;)
Rispondo subito con la possibilità c'è.

C'è la API GetNumberFormat.
La struttura da passarle è:

Private Type NUMBERFMT
NumDigits As Long
LeadingZero As Long
Grouping As Long
lpDecimalSep As String
lpThousandSep As String
NegativeOrder As Long
End Type

Non l'ho mai utilizzata ma dovrebbe funzionare.

P8257 WebMaster
25-06-2004, 12.21.53
(Y) Grazie :), è nella user32?

Bye :cool:

LoryOne
25-06-2004, 12.23.42
No, kernel32.
Attento.

P8257 WebMaster
25-06-2004, 12.26.42
LoryOne
No, kernel32.
Attento.

O.K. (Y) :)

Bye :cool:

NS-1
25-06-2004, 12.34.13
grazie...

quindi per ogni "acquisizione" devo settare i parametri...
non c'è una funzione globale?

NS-1
25-06-2004, 12.53.26
Public Declare Function GetNumberFormat Lib "kernel32" Alias "GetNumberFormatA" (ByVal Locale As Long, ByVal dwFlags As Long, ByVal lpValue As String, lpFormat As NUMBERFMT, ByVal lpNumberStr As String, ByVal cchNumber As Long) As Long



giusto no?

e adesso come imposto momentaneamente il formato 1,234,512.21312321?

LoryOne
25-06-2004, 12.55.19
Lascia perdere l'API ed utilizza Format$

NS-1
25-06-2004, 13.25.18
perchè?

LoryOne
25-06-2004, 13.49.06
Ripeto:
GetNumberFormat non l'ho mai usata ma mi pare di aver letto sulla bibbia di Appleman che la stringa numerica da formattare non debba contenere separatori delle migliaia.

A me pare che a te serva trasformare in un valore numerico una stringa di caratteri, cioè l'opposto.
Potrei anche sbagliarmi, questo pomeriggio ci do un'occhiata.

LoryOne
25-06-2004, 13.50.39
Aspetta.
Ho trovato questo:

· Locale
Specifies the locale for which the number string is to be formatted. If lpFormat is NULL, the function formats the string according to the number format for this locale. If lpFormat is not NULL, the function uses the locale only for formatting information not specified in the NUMBERFMT structure (for example, the locale’s string value for the negative sign).
This parameter can be a locale identifier created by the MAKELCID macro, or one of the following predefined values:
LOCALE_SYSTEM_DEFAULT
Default system locale.
LOCALE_USER_DEFAULT
Default user locale.

· dwFlags
Contains a bit flag that controls the operation of the function. If lpFormat is non-NULL, this parameter must be zero.
If lpFormat is NULL, you can specify the LOCALE_NOUSEROVERRIDE flag to format the string using the system default number format for the specified locale; or you can specify zero to format the string using any user overrides to the locale’s default number format

· lpValue
Points to a null-terminated string containing the number string to format.
This string can only contain the following characters:
· Characters ‘0’ through ‘9’
· One decimal point (dot) if the number is a floating-point value
· A minus sign in the first character position if the number is a negative value
All other characters are invalid. The function returns an error if the string pointed to by lpValue deviates from these rules.

· lpFormat
Pointer to a NUMBERFMT structure that contains number formatting information. All members in the structure pointed to by lpFormat must contain appropriate values.
If lpFormat is NULL, the function uses the number format of the specified locale.

· lpNumberStr
Points to a buffer to receive the formatted number string.

· cchNumber
Specifies the size, in bytes (ANSI version) or characters (Unicode version), of the lpNumberStr buffer. If cchNumber is zero, the function returns the number of bytes or characters required to hold the formatted number string, and the buffer pointed to by lpNumberStr is not used.

Prova e poi riposta per farmi sapere se ho preso un granchio, ok ?

NS-1
25-06-2004, 14.12.07
ok, grazie 200000000.... provo e ti faccio sapere... :D



p.s. ti ho mai detto che sei una potenza col VB? :D

LoryOne
25-06-2004, 14.17.26
Ce devo lavorà più che altro.

NS-1
25-06-2004, 14.49.16
e quindi cosa fà?

cambia le impostazioni internzionali sostituendo a mio piacimento l'utilizzo di virgole e punti?

:rolleyes:

NS-1
25-06-2004, 17.10.20
LoryOne:
E' il valore di tutto ciò he precede la virgola & "." & tutto ciò che viene dopo

Es:
a$="534,32"
Print Val(Left$(a$, InStr(a$, ",") - 1) & "." & Mid$(a$, InStr(a$, ",") + 1))


...
Ma non è meglio: stringa = Replace(stringa, ",", ".")?

P8257 WebMaster
25-06-2004, 17.28.58
NS-1 ha scritto
LoryOne:
E' il valore di tutto ciò he precede la virgola & "." & tutto ciò che viene dopo

Es:
a$="534,32"
Print Val(Left$(a$, InStr(a$, ",") - 1) & "." & Mid$(a$, InStr(a$, ",") + 1))


...
Ma non è meglio: stringa = Replace(stringa, ",", ".")?

.. Ma sei stato tu a dire che non volevi usare i replace.... :D...

fa caldo oggi eh? :D.. ;)

Bye :cool:

LoryOne
25-06-2004, 18.28.48
Ok, ricordavo bene infatti.
L'API GetNumberFormat necessita trasforma una stringa in un altra stringa, sebbene formattata in base ai parametri impostati nella struttura NUMBERFMT.

E' da qui che NS-1 ha finalmente svelato in toto il suo problema:

e adesso come imposto momentaneamente il formato 1,234,512.21312321?


Nel suo caso la questione è vista al contrario, ossia:
Ricavare un valore numerico da una stringa formattata in un certo modo.

NS-1:
Perchè ti ho suggerito di utilizzare Format$ ?
Beh, per un motivo essenziale:
Perchè il risultato fornito è di tipo variant e quindi molto comodo quando è necessario operare calcoli su cifre intere o in virgola mobile.
Al tempo però. E' di tipo variant se non dichiarato in precedenza.
(Ai puristi del codice questo può sembrare un'aberrazione, visto che il risultato dovrebbe essere di tipo stringa ma fa parte di una delle tante comodità di VB)
Es:

a$="123.456,03"
Print Format$(a$,"#,#0.00") * 2 //il risultato è il doppio di quello impostato ma variant

Es:

Dim b As Single
a$="123.456,03"
b=Format$(a$,"#,#0.00") * 2 //il risultato è il doppio di quello impostato ma single questa volta
Print b

Dove risiede il problema allora ?
Il problema risiede nelle impostazioni internazionali del S.O. (Kernel32.dll)
Format$ è indissolubilmente legato ad esse, tant'è che l'unica soluzione è effettuare un replace dei caratteri "," con "." e dei caratteri "." con "," così' come sono stati impostati nelle impostazioni internazionali di Windows stesso; infine usare Format$ per rispettare gli interi ed i decimali nel calcolo

Il formato è solo un modo di interpretare un risultato
Quando esegue i calcoli un computer se ne sbatte altamente dei separatori di migliaia o di quale carattere sia utilizzato per separare i decimali.

se a$="123.456,03" lui lo considererà SEMPRE come 123456.03

Non hai scelta amico mio. :(

LoryOne
25-06-2004, 18.29.45
Originariamente inviato da P8257 WebMaster


.. Ma sei stato tu a dire che non volevi usare i replace.... :D...

fa caldo oggi eh? :D.. ;)

Bye :cool:

Ogni tanto al Web piace ridere un po.
Benissimo, si è più produttivi se di buon umore :D :)

NS-1
26-06-2004, 11.56.50
:D
:D
:D
:D
:D
:D
:D

LoryOne
26-06-2004, 12.03.13
Cose' é ?
Hai creato S.O. distribuito che sei così di buon umore NS-1 ? :D :D

NS-1
26-06-2004, 13.12.15
:D
no, sono contento per il vostro aiuto le vostre battute e perchè piano piano imparo un sacco di cose...

:D

fino a 2 mesi fa non sapevo cosa farmene delle api e ora per qualsiasi cosa provo ad utilizzarle con risultati discreti... :D


grazie ancora per l'aiuto... (B)