COBOL : COMPUTE ROUNDED

Une petite notion de COBOL pour par exemple trouver quelques centimes par ci, quelques centimes par là.

Il s’agit de l’utilisation du paramètre ROUNDED dans l’écriture d’un COMPUTE

Les variables

01 W-NOMBRE1 PIC S9(15)V99 COMP-3.
01 W-NOMBRE2 PIC S9(15)V99 COMP-3.
01 W-MULTIPLICATEUR PIC S9(3)V9(6) COMP-3.
01 W-RESULTAT1 PIC S9(15)V99 COMP-3.
01 W-RESULTAT2 PIC S9(15)V99 COMP-3.
01 W-EDIT9 PIC +ZZZ.ZZZ.ZZZ.ZZZ.ZZ9,99.

Le code d’exemple

DISPLAY '----------ROUNDED DEBUT -------------'
MOVE 123,12 TO W-NOMBRE1
MOVE 567,56 TO W-NOMBRE2
MOVE 456,456789 TO W-MULTIPLICATEUR
MOVE 0 TO W-RESULTAT1
MOVE 0 TO W-RESULTAT2

COMPUTE W-RESULTAT1 ROUNDED = W-NOMBRE1
* W-NOMBRE2
* W-MULTIPLICATEUR

COMPUTE W-RESULTAT2 = W-NOMBRE1
* W-NOMBRE2
* W-MULTIPLICATEUR

MOVE W-RESULTAT1 TO W-EDIT9
DISPLAY 'ROUNDED    : ' W-EDIT9

MOVE W-RESULTAT2 TO W-EDIT9
DISPLAY 'NOT ROUNDED: ' W-EDIT9

MOVE 0 TO W-RESULTAT1
MOVE 0 TO W-RESULTAT2

COMPUTE W-RESULTAT1 ROUNDED = W-NOMBRE1
* W-NOMBRE2
- W-MULTIPLICATEUR

COMPUTE W-RESULTAT2 = W-NOMBRE1
* W-NOMBRE2
- W-MULTIPLICATEUR

MOVE W-RESULTAT1 TO W-EDIT9
DISPLAY 'ROUNDED    : ' W-EDIT9

MOVE W-RESULTAT2 TO W-EDIT9
DISPLAY 'NOT ROUNDED: ' W-EDIT9

DISPLAY '----------ROUNDED FIN -------------'

La sysout

----------ROUNDED DEBUT -------------
ROUNDED    : + 31.896.281,66
NOT ROUNDED: + 31.896.281,65
ROUNDED    : + 69.421,53
NOT ROUNDED: + 69.421,53
----------ROUNDED FIN -------------

Résultats réels

Pour le premier calcul : 31.896.281,6590951

Pour le second : 69421,530411

Conclusion

Comme l’explique la documentation Cobol, ROUNDED arrondi la dernière décimale en l’augmentant de 1 si la décimale suivante en trop dépasse ou est égale à 5. Vrai dans le premier calcul faut dans le second.

Notez que le comportement normal est la troncature des décimaux en trop par rapport à la variable de réception du COMPUTE.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *