// Serge GOMES // P R O T O T Y P E /IF DEFINED(SGFORMAT) /EOF /ENDIF /DEFINE SGFORMAT *-------------------------------------------------- * Procedure name: FormatS * Purpose: Formater un nombre en chaine * Returns: Chaine de 40 caractères 1 * Parameter: P_Val_Ori => Valeur d'origine 2 * Parameter: P_Lng => Longeur totale attendue 3 * Parameter: P_Dec => Nombre de décimales 4 * Parameter: P_Zero => "<"=>Supprime zéros de gauche * ">"=>Supprime zéros droite * "-"=>Supprime tous les zéros * " "=>Ne Supprime pas les zéros 5 * Parameter: P_subst => caractère de substitution des zéros * sans effet si P_Zero = *blank 6 * Parameter: P_Moins => Gestion du signe * *OFF=>pas de signe * *ON=>signe - devant 7 * Parameter: P_Sep_Dec => Séparateur décimale 8 * Parameter: P_Sep_Mil => Séparateur millier * => '0' pas de séparateur * => sinon le caractère transmis * Exemple=> W_A40 = FORMATS(W_Orig3:8:2:'-':*ON:' ':'.':' '); * *-------------------------------------------------- D FormatS PR 40A 1 D P_Val_Ori 30P 9 VALUE 2 D P_Lng 2P 0 VALUE OPTIONS(*NOPASS) 3 D P_Dec 1P 0 VALUE OPTIONS(*NOPASS) 4 D P_Zero 1A VALUE OPTIONS(*NOPASS) D* Valeur par défaut '-' 5 D P_Moins N VALUE OPTIONS(*NOPASS) D* Valeur par défaut *on 6 D P_Subst 1A VALUE OPTIONS(*NOPASS) D* Valeur par défaut *blank 7 D P_Sep_Dec 1A VALUE OPTIONS(*NOPASS) D* Valeur par défaut ',' 8 D P_Sep_Mil 1A VALUE OPTIONS(*NOPASS) D* Valeur par défaut 0 // CODE SOURCE * Programme de service Formatage texte * CRTRPGMOD MODULE(LIB/SGFORMAT) * * CRTSRVPGM SRVPGM(LIB/SGFORMAT) *--------------------------------------------------------------------------------------------- H NoMain decedit('0,') DATEDIT(*YMD) /COPY PROTOTYPE,SGFORMAT D FormSep PR 40A 1 D P_Chaine 40A VALUE 2 D P_Sep_Dec 1A VALUE 8 D P_Sep_Mil 1A VALUE P FormatS B EXPORT D FormatS PI 40A 1 D P_Val_Ori 30P 9 VALUE 2 D P_Lng 2P 0 VALUE OPTIONS(*NOPASS) 3 D P_Dec 1P 0 VALUE OPTIONS(*NOPASS) 4 D P_Zero 1A VALUE OPTIONS(*NOPASS) 5 D P_Moins N VALUE OPTIONS(*NOPASS) 6 D P_Subst 1A VALUE OPTIONS(*NOPASS) 7 D P_Sep_Dec 1A VALUE OPTIONS(*NOPASS) 8 D P_Sep_Mil 1A VALUE OPTIONS(*NOPASS) * Variables de travail DW_A30 Ds 30 INZ DW_Ent 21 OVERLAY(W_A30:1) DW_Deci 9 OVERLAY(W_A30:22) DW_NDec 9S 0 OVERLAY(W_A30:22) ** D W_Retour s 40A inz(*blank) D W_I s 2 0 D W_Deb s 2 0 inz(0) D W_Dec s like(W_NDec) D W_Sep_Dec s like(P_Sep_Dec) inz D W_Val_Ori s like(P_Val_Ori) *--------------------------------------------------------------------------------------------- /FREE If %parms < 8; P_Sep_Mil = '0'; EndIf; If %parms < 7; P_Sep_Dec = ','; EndIf; If %parms < 6; P_Subst = *blank; EndIf; If %parms < 5; P_Moins = *on ; EndIf; If %parms < 4; P_Zero = '-'; EndIf; If %parms < 3; P_Dec = 9; EndIf; If %parms < 2; P_Lng = 30; EndIf; // Traitement spécial pour P_Sep_Dec = *blank if P_Sep_Dec = *blank and P_Sep_Mil <> '0'; W_Sep_Dec = 'µ'; else; W_Sep_Dec = P_Sep_Dec; EndIf; If P_Val_Ori < 0 ; W_Val_Ori = - P_Val_Ori; Else ; W_Val_Ori = P_Val_Ori; EndIf; W_A30 = %trimL(%editc(W_Val_Ori:'X')); W_Dec = W_NDec; if P_Zero = '<' or P_Zero = '-' ; // suppression des 0 non significatifs de gauche W_I = 1 ; Dow (%subst(W_Ent:W_I:1) = '0' OR %subst(W_Ent:W_I:1) = *blank) AND W_I <= %len(W_Ent) - 1; %subst(W_Ent:W_I:1) = P_Subst; W_I=W_I+1; EndDo; EndIf; if P_Zero = '>' or P_Zero = '-' ; // suppression des 0 non significatifs de droite W_I = %len(W_Deci); Dow W_I > 0 and ((%subst(W_Deci:W_I:1) = '0' OR %subst(W_Deci:W_I:1) = *blank)); %subst(W_Deci:W_I:1) = P_Subst; W_I=W_I-1; EndDo; EndIf; // Partie décimale If (W_Dec > 0 and P_Dec > 0 ) or (P_Dec > 0 and P_Zero <> '>' and P_Zero <> '-'); W_Retour = W_Sep_Dec + %subst(W_Deci:1:P_Dec); EndIf; // recherche début (partie entière) W_Deb = %len(W_Ent) - (P_Lng - P_Dec) + 1; W_Retour = %trim(%subst(W_Ent:W_Deb:P_Lng - P_Dec))+ %Trim(W_Retour) ; if P_sep_Mil <> '0'; // séparateur de millier W_Retour = FormSep(W_Retour:W_Sep_Dec:P_Sep_Mil); EndIf; If P_Val_Ori < 0 and P_Moins; // Gestion du signe W_Retour = '-' + W_Retour; EndIf; // Rétablissement séparateur décimal (surtout si *blank) if P_Sep_Dec <> W_Sep_Dec; W_Retour = %xlate(W_Sep_Dec:P_Sep_Dec:W_Retour); EndIf; return W_Retour ; /END-FREE P FormatS E *--------------------------------------------------------------------------------------------- P FormSep B D FormSep PI 40A 1 D P_Chaine 40A value 2 D P_Sep_Dec 1A value 3 D P_Sep_Mil 1A VALUE * Variables de travail D W_Deb s 2 0 inz(0) D W_Fin s 2 0 inz(0) D W_Lng s 2 0 inz(0) D W_Tmp s 2 0 inz(0) D W_Rest s 2 0 inz(0) D W_x s 2 0 inz(1) D W_ind s 2 0 inz(1) D DS DW_Ch like(P_Chaine) DW_Tab 1 dim(40) inz(*blank) D overlay(W_Ch) *--------------------------------------------------------------------------------------------- /FREE W_Ind = 1; W_Ch = *blank; W_Lng = %len(%trim(P_Chaine)); W_Deb = %scan(P_Sep_Dec:P_Chaine); if W_Deb > 0 and W_Deb < W_Lng ; // Il faut traiter la partie entière et la partie décimale W_Fin = W_Deb - 1 ; else; W_Fin = W_Lng; EndIf; W_x = 1; Dow W_x <= W_Fin; W_Tmp = W_Fin - W_x; W_Rest = %rem(W_Tmp:3); W_Tab(W_Ind) = %subst(P_Chaine:W_x:1) ; W_Ind = W_Ind + 1 ; if W_Rest = 0 and W_x <> W_Fin; W_Tab(W_Ind) = P_Sep_Mil; W_Ind = W_Ind + 1; EndIf; W_x = W_x + 1; EndDo; W_x = 1; if W_Deb > 0 and W_Deb < W_Lng ; // partie décimale W_Tmp = 0; W_x = W_Deb + 1; W_Fin = W_Lng; W_Tab(W_Ind) = P_Sep_Dec; W_Ind = W_Ind + 1 ; Dow W_x <= W_Fin; W_Tab(W_Ind) = %subst(P_Chaine:W_x:1) ; W_Ind = W_Ind + 1 ; W_Tmp = W_Tmp + 1; if W_Tmp = 3; W_Tmp = 0; W_Tab(W_Ind) = P_Sep_Mil; W_Ind = W_Ind + 1; EndIf; W_x = W_x + 1; EndDo; EndIf; return W_Ch ; /END-FREE P FormSep E // Source du liage (QSRVSRC) STRPGMEXP PGMLVL(*CURRENT) SIGNATURE('SGFORMAT_V01') EXPORT SYMBOL("FORMATS") ENDPGMEXP