PrintUsing()
Publié : sam. 21/janv./2012 11:40
J'ai repris le code de Brossden pour lui ajouter plusieurs fonctions de formatage de chaine.
Je l'ai debuggée au maximum et j'attends le résultat de vos essais.
Mesa.
Je l'ai debuggée au maximum et j'attends le résultat de vos essais.
Mesa.
Code : Tout sélectionner
;================================================================================================================================================
; Fonction PRINT USING
;
; PrintUsing()
;
; Syntaxe
;
; Resultat = PrintUsing(Chaine$, Format$, [Options.b, [Virgule$]])
; Result = PrintUsing(String$, Format$, [Options.b, [Coma$]])
;
; Description
;
; Renvoie la représentation de la chaine de caractères Chaine$ correspondant au masque spécifié.
; Masque$ est une chaîne indiquant comment formater la chaine de caractères.
; Il est possible d'utiliser les identifiants suivants: [[Prefixe][#][séparateur]][#][.][#]]
;
; Paramètre facultatifs : Options
; #PB_PrintUsing_Defaut : Aucune option.
; #PB_PrintUsing_SignPlus : Affiche le signe "+" devant le nombre.
; #PB_PrintUsing_NoDecimal : N'affiche pas la partie décimale du nombre.
; Utile pour afficher un numéro de téléphone ave le Point (".") comme séparateur.
; #PB_PrintUsing_ForceZero : Force le remplissage du format avec des "0" si nécessaire.
; #PB_PrintUsing_ForceEtoile : Force le remplissage du format avec des "*" si nécessaire.
;
; Paramètre facultatifs : Virgule$
; Par défaut la virgule est représentée par "." dans le resultat du formatage.
; N'importe quel caractère ou une chaine de caractères peut être utilisé.
; Ex: Virgule$ = "," : La virgule est représentée par "," dans le resultat du formatage
; Virgule$ = " = "
; Virgule$ = " est en hausse de "
;
; Remarque : Un préfixe est une chaine de caractère quelconque.
; Ex: "Tel : ##.##.##.##.##.##,#PB_PrintUsing_NoDecimal"
;
; Remarque : #PB_PrintUsing_ForceZero et #PB_PrintUsing_ForceEtoile
; ne tiennent pas compte du séparateur.
;
;
; Exemple
; Debug PrintUsing("-1234.00,"### ###") => -1 234
; Debug PrintUsing("-1234,"### ###.##") => -1 234.00
; Debug PrintUsing("-1234.567,"### ###.##") => -1 234.56
; Debug PrintUsing("1234567890,"### ###.## €,#PB_PrintUsing_Defaut,",") => 1 234 567 890,00 €
; Debug PrintUsing("1234567890.56,"#.### %,#PB_PrintUsing_SignPlus,",") => +1234567890,560 %
; Debug PrintUsing("0102030405,"Tel :##.##.##.##.##.##,#PB_PrintUsing_NoDecimal,",") => Tel : 01.02.03.04.05
; Debug PrintUsing("190120121845,"##/##/#### ##h ##',",") => 19/01/2012 18h 45'
; Debug PrintUsing("1234567890.56,"### ###.### %,#PB_PrintUsing_SignPlus,"<-@->") => +1 234 567 890<-@->560 %
; Debug PrintUsing("1234567890.5,"$###,###.##,",") => $1,234,567,890.50
; Debug PrintUsing("12345678905258,"##/## ##:##,",") => 12/34/56/78/90 52:58
; Debug PrintUsing("12.56,"###.##,#PB_PrintUsing_ForceZero,",") => 012.56
; Debug PrintUsing("12.56,"######.##,#PB_PrintUsing_ForceEtoile,",") => ****12.56
; Debug PrintUsing("12.56,"### ###.##,#PB_PrintUsing_SignPlus|#PB_PrintUsing_ForceZero,",") => +000012.56
;
;
;
; OS Supportés
;
; Tous
;
;================================================================================================================================================
#PB_PrintUsing_Defaut = 1 << 0
#PB_PrintUsing_SignPlus = 1 << 1
#PB_PrintUsing_NoDecimal = 1 << 2
#PB_PrintUsing_ForceZero = 1 << 3
#PB_PrintUsing_ForceEtoile = 1 << 4
;#PB_PrintUsing_Scientific = 1 << 5 To Do / Reste à faire 1.e-12 ou 1.10^-12
Procedure.s PrintUsing(Chaine.s, Format.s, Options.b=0, Virgule.s=".")
;Pour mémoire ===========================
; #PB_PrintUsing_Defaut = 1 << 0
; #PB_PrintUsing_SignPlus = 1 << 1
; #PB_PrintUsing_NoDecimal = 1 << 2
; #PB_PrintUsing_ForceZero = 1 << 3
; #PB_PrintUsing_ForceEtoile = 1 << 4
;========================================
;--------------------------- Options #PB_PrintUsing_SignPlus et gestion du Signe "-" ----------------------------------------------------------
Sign.s = "" ; permet d'afficher le signe - ou le signe + ou pas de signe
If Options & #PB_PrintUsing_SignPlus ;Affichage du signe +
Sign="+"
EndIf
If Left(Chaine,1)="-" ;Affichage du signe -
Sign = "-"
Chaine = Mid(Trim(Chaine),2)
EndIf
;---------------------------Les espaces en début et fin sont éliminés -------------------------------------------------------------------------
Trim_Chaine.s = Trim(Chaine)
Trim_Format.s = Trim(Format)
;--------------------------- Prefixe présent ? ------------------------------------------------------------------------------------------------
; Préfixe de type $###.. ou Tel :##...
Longueur_Prefix = 1
Prefix.s = ""
While Mid(Trim_Format,Longueur_Prefix,1) <> "#"
Longueur_Prefix = Longueur_Prefix + 1
Wend
Longueur_Prefix = Longueur_Prefix - 1
Prefix = Mid(Trim_Format,1,Longueur_Prefix)
;Debug Prefix
If Longueur_Prefix >0
Trim_Format = Mid(Trim_Format,Longueur_Prefix+1)
;Debug Trim_Format
EndIf
;--------------------------- Position de la virgule -------------------------------------------------------------------------------------------
;Pour ne pas confondre la virgule "." avec un séparateur ".", on inverse la chaine de caractère
;Position Virgule dans la Chaine
Position_Virgule_Chaine = 1 + Len(Trim_Chaine) - FindString(ReverseString(Trim_Chaine),".",1)
;Cas des nombres entiers Position Virgule
If Position_Virgule_Chaine = 0 : Position_Virgule_Chaine = Len(Trim_Chaine)+1 : EndIf
;Position Virgule dans le Format
Position_Virgule_Format = 1 + Len(Trim_Format) - FindString(ReverseString(Trim_Format),".",1);FindString(Format,".",1)
;Cas des nombres entiers Position Virgule
If Position_Virgule_Format = 0 : Position_Virgule_Format = Len(Trim_Format)+1 : EndIf
;--------------------------- Nombre de caractères ---------------------------------------------------------------------------------------------
Nbre_Caractere_Format_Partie_Entiere = CountString(Mid(Trim_Format,1,Position_Virgule_Format),"#") ; partie entière Format
Nbre_Caractere_Format_Partie_Decimale = CountString(Mid(Trim_Format,Position_Virgule_Format),"#") ; partie décimale Format
Nbre_Caractere_Chaine_Partie_Entiere = Position_Virgule_Chaine-1 ;longueur partie entiere chaine
;Nbre_Caractere_Chaine_Partie_Decimale =
;--------------------------- Position du séparateur -------------------------------------------------------------------------------------------
; 1 Seul séparateur est géré !
Position_Separateur = 1
While Mid(Trim_Format,Position_Separateur,1) = "#"
Position_Separateur + 1
Wend
;Ne sert A RIEN ?
Separateur.s=Mid(Trim_Format,Position_Separateur,1);:Debug Separateur
;Partie entière : Répétition d'un groupe de caractère dans le format ?
;### ###.## = groupe1 + separateur + groupe2 + "." + groupe3
; et Format_Partie_Entiere = groupe1 + separateur + groupe2
Format_Groupe1.s= Mid(Trim_Format,1,Position_Separateur);:Debug Format_Groupe1
;Ne pas confondre virgule "." et absence de separateur
If Position_Virgule_Format = Position_Separateur
Format_Groupe1 = Mid(Format_Groupe1,1,Len(Format_Groupe1)-1)
EndIf
Format_Partie_Entiere.s = Mid(Trim_Format,1,Position_Virgule_Format-1);:Debug Format_Partie_Entiere
Format_Partie_Decimale.s = Mid(Trim_Format,Position_Virgule_Format+1);:Debug Format_Partie_Decimale
;si Nbre_Caractere_Chaine_Partie_Entiere > Nbre_Caractere_Format_Partie_Entiere
;alors on crée un format en répétant le motif groupe1
N = Nbre_Caractere_Format_Partie_Entiere
While N < Nbre_Caractere_Chaine_Partie_Entiere
Format_Partie_Entiere = Format_Groupe1 + Format_Partie_Entiere;:Debug Format_Partie_Entiere
N + Position_Separateur -1
Wend
;----------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------- RESULTAT -----------------------------------------------------------------------------------------------
;----------------- Paramètre Facultatif #PB_PrintUsing_forceZero Si la chaine est trop courte alors remplir avec des zéros --------------------
If Options & #PB_PrintUsing_ForceZero ;
Forcer_Zero = 1
Else
Forcer_Zero = 0
EndIf
;-----------------Paramètre Facultatif #PB_PrintUsing_forceEtoile Si la chaine est trop courte,alors remplir avec des "*" ---------------------
If Options & #PB_PrintUsing_ForceEtoile ;
Forcer_Etoile = 1
Else
Forcer_Etoile = 0
EndIf
;--------------------------- Partie Décimale avec des 0 ---------------------------------------------------------------------------------------
; ajouter 0 en partie decimal si necessaire
; Resultat_Partie_Decimale.s = LSet(Mid(Trim_Chaine,Position_Virgule_Chaine+1),Nbre_Caractere_Format_Partie_Decimale,"0")
NchaineDecimale.s = Mid(Trim_Chaine,Position_Virgule_Chaine+1); partie decimale chaine
Resultat_Partie_Decimale.s ;= Chaine_Partie_Decimale
CarCh =1
; Lire le Format, caractère par caractère
Nchaine.s = Mid(Trim_Chaine,1,Position_Virgule_Chaine-1); partie entiere chaine
For CarFo = 1 To Len(Format_Partie_Decimale)
Car.s = Mid(Format_Partie_Decimale, CarFo,1)
; Si c'est un "#" alors on place un "chiffre" de Chaine$
If Car ="#"
If Mid(NchaineDecimale,CarFo,1) = ""; Si la partie décimale de la chaine est trop courte
Resultat_Partie_Decimale = Resultat_Partie_Decimale + "0"; alors on ajoute un zéro
Else
Resultat_Partie_Decimale = Resultat_Partie_Decimale + Mid(NchaineDecimale,CarCh,1)
EndIf
CarCh+1
Else ; sinon on place le reste des caratères
Resultat_Partie_Decimale = Resultat_Partie_Decimale + Car
EndIf
Next CarFo
;--------------------------- Paramètre facultatif Virgule$-------------------------------------------------------------------------------------
If Nbre_Caractere_Format_Partie_Decimale > 0 ;Faut-il mettre une virgule
If Virgule <>"." ;Si oui alors...
Resultat_Partie_Decimale = Virgule + Resultat_Partie_Decimale ; virgule personnalisée
Else
Resultat_Partie_Decimale = "." + Resultat_Partie_Decimale ; virgule "."
EndIf
EndIf
;----------------------------- Partie Entière -------------------------------------------------------------------------------------------------
Prefixe_de_Remplissage.s=""
If Forcer_Zero = 1 ; si paramètre facultatif #PB_PrintUsing_ForceZero alors remplir de zéros
If Nbre_Caractere_Format_Partie_Entiere > Nbre_Caractere_Chaine_Partie_Entiere ; Faut-il remplir de zéros ?
For i=1 To Nbre_Caractere_Format_Partie_Entiere - Nbre_Caractere_Chaine_Partie_Entiere ; si oui, de combien de zéros ?
Prefixe_de_Remplissage + "0"
Next i
EndIf
EndIf
If Forcer_Etoile = 1 ; si paramètre facultatif #PB_PrintUsing_ForceEtoile alors remplir des "*"
Prefixe_de_Remplissage="" ; au cas où déjà rempli avec des 0
If Nbre_Caractere_Format_Partie_Entiere > Nbre_Caractere_Chaine_Partie_Entiere ; Faut-il remplir de zéros ?
For i=1 To Nbre_Caractere_Format_Partie_Entiere - Nbre_Caractere_Chaine_Partie_Entiere ; si oui, de combien de zéros ?
Prefixe_de_Remplissage + "*"
Next i
EndIf
EndIf
If Forcer_Zero = 1 And Forcer_Etoile = 1 ; Avertir qu'on ne peut pas utiliser les 0 et * en même temps
MessageRequester("Erreur", "Les paramètres #PB_PrintUsing_ForceZero et #PB_PrintUsing_ForceEtoile sont incompatible ensemble.", 16)
EndIf ; * est prioritaire
Resultat_Partie_Entiere.s ;= Chaine_Partie_Decimale
CarCh =1
; Lire le Format, caractère par caractère
For CarFo = 1 To Len(Format_Partie_Entiere)
Car.s = Left(Right(Format_Partie_Entiere,CarFo),1)
; Si c'est un "#" alors on place un "chiffre" de Chaine$
If Car ="#"
Resultat_Partie_Entiere = Left(Right(Nchaine,CarCh),1) + Resultat_Partie_Entiere
CarCh+1
Else ; sinon on place le separateur
Resultat_Partie_Entiere = Car + Resultat_Partie_Entiere
EndIf
If CarCh > Len(Nchaine)
CarFo = Len(Format_Partie_Entiere)
EndIf
Next CarFo
;----------------------------- Paramètre facultatif Sans_Partie_Décimale ----------------------------------------------------------------------
If Options & #PB_PrintUsing_NoDecimal ; Ne pas tenir compte de la partie décimale
Resultat_Partie_Decimale = ""
EndIf
;____________________________ Affichage du Resultat ____________________________________________________________________________________________
ProcedureReturn Prefix + Sign + Prefixe_de_Remplissage + Resultat_Partie_Entiere + Resultat_Partie_Decimale
EndProcedure
;================================================================================================================================================
If OpenWindow(0,0,0,1000,600,"PrintUsing()",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
EditorGadget(0,10,10,980,580)
Font1 = LoadFont(#PB_Any, "Arial",10, #PB_Font_Bold)
SetGadgetFont(0, FontID(Font1))
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"-1234.00"+","+Chr(34)+"### ###"+Chr(34) +") => "+ PrintUsing("-1234.00","### ###"))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"-1234"+","+Chr(34)+"### ###.##"+Chr(34) +") => "+ PrintUsing("-1234","### ###.##"))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"-1234.567"+","+Chr(34)+"### ###.##"+Chr(34) +") => "+ PrintUsing("-1234.567","### ###.##"))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"1234567890"+","+Chr(34)+"### ###.## €,#PB_PrintUsing_Defaut,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("1234567890","### ###.## €",#PB_PrintUsing_Defaut,","))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"1234567890.56"+","+Chr(34)+"#.### %,#PB_PrintUsing_SignPlus,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("1234567890.56","#.### %",#PB_PrintUsing_SignPlus,","))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"0102030405"+","+Chr(34)+"Tel :##.##.##.##.##.##,#PB_PrintUsing_NoDecimal,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("0102030405","Tel : ##.##.##.##.##.##",#PB_PrintUsing_NoDecimal,"."))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"190120121845"+","+Chr(34)+"##/##/#### ##h ##',"+Chr(34) +","+Chr(34) +") => " + PrintUsing("190120121845","##/##/#### ##h ##'"))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"1234567890.56"+","+Chr(34)+"### ###.### %,#PB_PrintUsing_SignPlus,"+Chr(34)+"<-@->"+Chr(34)+ ") => " + PrintUsing("1234567890.56","### ###.### %",#PB_PrintUsing_SignPlus,"<-@->"))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"1234567890.5"+","+Chr(34)+"$###,###.##,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("1234567890.5","$###,###.##"))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"12345678905258"+","+Chr(34)+"##/## ##:##,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("12345678905258","##/## ##:##"))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"12.56"+","+Chr(34)+"###.##,#PB_PrintUsing_ForceZero,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("12.56","###.##",#PB_PrintUsing_ForceZero))
AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"12.56"+","+Chr(34)+"######.##,#PB_PrintUsing_ForceEtoile,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("12.56","######.##",#PB_PrintUsing_ForceEtoile))
AddGadgetItem(0, -1," ")
;AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"12.56"+","+Chr(34)+"######.##,#PB_PrintUsing_ForceZero|#PB_PrintUsing_ForceEtoile,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("12.56","######.##",#PB_PrintUsing_ForceZero|#PB_PrintUsing_ForceEtoile))
;AddGadgetItem(0, -1," ")
AddGadgetItem(0, -1,"PrintUsing("+Chr(34)+"12.56"+","+Chr(34)+"### ###.##,#PB_PrintUsing_SignPlus|#PB_PrintUsing_ForceZero,"+Chr(34) +","+Chr(34) +") => " + PrintUsing("12.56","### ###.##",#PB_PrintUsing_SignPlus|#PB_PrintUsing_ForceZero))
AddGadgetItem(0, -1," ")
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
; #PB_PrintUsing_Defaut = 1 << 0
; #PB_PrintUsing_SignPlus = 1 << 1
; #PB_PrintUsing_NoDecimal = 1 << 2
; #PB_PrintUsing_ForceZero = 1 << 3
; #PB_PrintUsing_ForceEtoile = 1 << 4
; #PB_PrintUsing_Scientific = 1 << 5 toDo
; MessageRequester("PrintUsing()","-1234.00 -> ### ###" + #LFCR$ + PrintUsing("-1234.00","### ###"))