Bonjour
C'est un sujet qui m'intéresse beaucoup, on en a déjà parlé et j'ai même précisé que ce problème persiste et ne sera jamais réglé par une simple ValD(X$) ça ne donnera jamais ce qu'on veut.
La solution la plus sûre c'est de créer des petites procédures pour régler ce genre de problème.
Je l'ai déjà fait et ça marche très bien. Il s'agit de créer des procédures bien distinctes car le string peut très bien contenir n'importe quel type de numérique (entier, décimal avec 1,2,3,4.. décimaux etc..) Voici en tout cas ce que j'ai fait et ce que je continue à faire jusqu'à présent.
Code : Tout sélectionner
Global wstring$, WMsg$,WLeft$, Wright$,wDecim$, WNew$, wVal$, Wsauve$
Global wtaille,WPos,wNbDecim, ErrorFound
Global Z,K$,i,j,k,DIM wCar$(30)
;-------------------------------- Conversion Entier vers Décimal ----------------------
Procedure Convertir_En_D()
;Enlever les espaces des milliers (éventuellement ex: 258 114 devient 258114)
WNew$=""
Wstring$=Trim(wString$)
For i=1 To Len(Wstring$)
WVal$=Mid(Wstring$,i,1)
If WVal$<>""
WNew$=WNew$+wVal$
EndIf
Next i
Wstring$=Trim(wnew$)
Wtaille=Len(wString$)
;Cas où le nbre est trop petit (ex: 1 centième)
If wtaille=1 : wString$="0.0" + Trim(wString$): EndIf
If wtaille=2 : wString$="0." + Trim(wString$): EndIf
If wtaille>2
WLeft$=Left(wString$,wtaille-2)
wDecim$=Right(wString$,2)
wString$=Trim(WLeft$+"."+wDecim$)
EndIf
If ValQ(wString$)=0 : wString$="0.00" : EndIf
If Left(wString$,1)="." Or Left(wString$,1)="," : wString$="0"+wString$ : EndIf
Wstring$=Trim(wString$)
EndProcedure
;-------------------------------- Conversion Décimal vers Entier ----------------------
Procedure Convertir_En_E()
WPos= FindString(Wstring$, ",", 1)
If wpos=0
wPos= FindString(Wstring$, ".", 1)
EndIf
If wpos=0
Wstring$=Wstring$+"00"
Else
wDecim$=Mid(Wstring$,wpos+1,2)
wLeft$=Left(Wstring$,wpos-1)
wRight$=LSet(wDecim$,2,"0")
Wstring$=Trim(wLeft$)+Trim(wRight$)
EndIf
Wstring$=Trim(wString$)
EndProcedure
;-------------------------------- Séparer numeric en milliers --------------------
Procedure Separer_Numeric_En_Millier()
;Enlever éventuellement les espaces internes
Wsauve$=""
wstring$=Trim(wstring$)
Wtaille=Len(wstring$)
For i=1 To wtaille
If Mid(wstring$,i,1)<>Space(1)
Wsauve$=Wsauve$+ Mid(wstring$,i,1)
EndIf
Next i
wstring$=Trim(wsauve$)
For i=1 To 22 : wCar$(i)="" : Next i
i=22
Wtaille=Len(wstring$)
wRight$=""
If Mid(wString$,wtaille-2,1)="." Or Mid(wString$,wtaille-2,1)=","
wRight$=Space(1) + Right(wstring$,6)
wtaille=wtaille-6
EndIf
While i>0
For j=1 To 3
If wtaille>0
wCar$(i)=Mid(wString$,wtaille,1)
wtaille=wtaille-1
i=i-1
EndIf
If wtaille<=0
Break
EndIf
Next j
i=i-1
If i>0
wCar$(i)=Space(1)
i=i-1
EndIf
Wend
wstring$=""
For i=1 To 22
wstring$=wstring$+wCar$(i)
Next i
wstring$=wstring$+wright$
wstring$=Trim(wstring$)
EndProcedure
;-------------------------------- Test si Numérique --------------------
Procedure IsNumeric(wString$)
wString$=Trim(wString$)
wTaille=Len(wString$)
wNbDecim=0
k$=""
Result=0
For z=1 To wTaille
k$=Mid(wString$,z,1)
If k$="1" Or k$="2" Or k$="3" Or k$="4" Or k$="5" Or k$="6" Or k$="7" Or k$="8" Or k$="9" Or k$="0" Or k$="," Or k$="."
Result=1
Else
Result=0
Break
EndIf
If k$="," Or k$="."
wNbDecim=wNbDecim+1
If wNbDecim>1
Result=0
Break
EndIf
EndIf
Next z
ProcedureReturn Result
EndProcedure
;-------------------------------- Test si NbreDécimal --------------------
Procedure NbreDecimal(wString$)
wString$=Trim(wString$)
wTaille=Len(wString$)
wNbDecim=0
k$=""
Result=0
For z=1 To wTaille
k$=Mid(wString$,z,1)
If k$="1" Or k$="2" Or k$="3" Or k$="4" Or k$="5" Or k$="6" Or k$="7" Or k$="8" Or k$="9" Or k$="0" Or k$="," Or k$="."
Result=1
Else
Result=0
Break
EndIf
If k$="," Or k$="."
wNbDecim=wNbDecim+1
If wNbDecim>1
Result=0
Break
EndIf
EndIf
Next z
ProcedureReturn Result
EndProcedure
;-------------------------------- Test si NbreEntier --------------------
Procedure NbreEntier(wString$)
wString$=Trim(wString$)
wTaille=Len(wString$)
k$=""
Result=0
For z=1 To wTaille
k$=Mid(wString$,z,1)
If k$="1" Or k$="2" Or k$="3" Or k$="4" Or k$="5" Or k$="6" Or k$="7" Or k$="8" Or k$="9" Or k$="0"
Result=1
Else
Result=0
Break
EndIf
Next z
ProcedureReturn Result
EndProcedure
;-------------------------------- Caractères de nom de champ valide?----------------------
Procedure InvalidName(wString$)
wstring$=Trim(wstring$)
Result=0
wPos= FindString(Wstring$, ",", 1)
If wpos=0
wPos= FindString(Wstring$, ".", 1)
EndIf
If wpos=0
wPos= FindString(Wstring$, Space(1), 1)
EndIf
If wpos<>0
Result=1
EndIf
ProcedureReturn Result
EndProcedure
;***************************************************************
; TEST
;*************************************************************
;Tester si la chaîne est correcte (type quelconque mais correct)
WString$=getGadGetText(#Chaîne)
;1. Je teste si cette chaîne contient des caractères valides d'abord
IF InvalidName(wString$)
wMsg$="Chaîne incorrecte !"
Goto Retour
EndIF
;2. Je teste si cette chaîne est numérique
IF not IsNumeric(wString$)
wMsg$="Chaîne non numérique !"
Goto Retour
EndIF
;3. Je teste s'il s'agit d'un entier
IF NbreEntier(wString$)
wMsg$="C'est un entier !"
EndIF
;4. Je teste s'il s'agit d'un nombre décimal
IF NbreDecimal(wString$)
wMsg$="C'est un nbre décimal !"
EndIF
;A PARTIR D'ICI JE PEUX FAIRE CE QUE JE VEUX AVEC MA VARIABLE
;Si j'ai un entier (je le sais maintenant), et que je veux l'afficher en séparant les milliers
; Exemple : 15823 devient 15 823, il suffit de faire ceci:
Separer_Numeric_En_Millier()
DeBug wString$
;Si je veux récupérer exactement les mêmes caractères du champ de saisie
; on a qu'à utiliser directement Wstring$ (sans conversion)
; exemple: Wstring$="10.000024"
;Si maintenant je veux travailler avec ce nombre, il suffit de le transférer directement
;dans la variable double (ou float):
MaVar.D= Wstring$
;Si je veux faire des calculs avec prise en charge de toutes les décimales on fera :
; a) Convertir ce nbre en entier
;(La procédure de conversion travaille avec 0 à 2 décimaux, donc ici je dois récupérer le reste des décimaux :)
WTaille=len(wstring$)
Wreste$=right(Wstring$,4) (on peut généraliser ce nbre 4)
WNbre$=left(wstring$,wtaille-4)
wstring$=WNbre$
Convertir_En_Entier()
Debug wstring$ (qui doit donner: "1000")
;On ajoute les autres décimaux
wstring$=wstring$=+Wreste$
Debug wstring$ (qui doit donner: "10000024")
;A partir de là je crois qu'on peux faire ce qu'on veut avec ce nbre
De toute façon c'est juste une exemple mais je sais qu'il doit y avoir d'autres solutions