Search code examples
rpglerpg

Display double in 2 decimal places in RPG?


When declaring floating-point variable, I don't understand the amount of precision needed for the stored object in RPG ??

In my exercice, I enter the number of copies for example the value 5.

enter image description here

The total amount is 0.50 euros but I have like message:

enter image description here

I don't understand how to declare the variable total correctly in double.

 H
 D NumberCopy      S              3S 0
 D Total           S              ???
  *
  /Free
    dsply 'Enter your copy number please : ' '' NumberCopy;

    If (NumberCopy < 11);
       Total = NumberCopy * 0.10;

    ElseIf (NumberCopy < 31);
       Total = (10 * 0.10) + (NumberCopy - 10) * 0.09;

    Else;
       Total = (10 * 0.10) + (20 * 0.09) + (NumberCopy - 30) * 0.08;

    EndIf;


    dsply ('The amount is of ' + %Char(Total) + ' euros');


    *inlr = *on;
  /End-Free

Here I find this on RPGPDM.

D Float1          S              8F 

https://www.rpgpgm.com/2014/02/defining-variables-in-rpg-all-free.html


Solution

  • to say it at the beginning of your exercise DONT USE FLOAT IN RPG. It will make you pretty unhappy. To answer your question: dcl-s total float(8); You have two problems with float:

    1. You dont want your customers to get wrong invoices due to accuracy problems like in 1990.
    2. Decimals are able too use hardware support to evaluate faster which isn't "important" but a nice to have.

    If you wanna display a float you can do something like that

    dcl-s outputString char(10) inz;
    dcl-s total float(8) inz (5);
    
    outputString = %char(%dec(total: 10: 2));
    dsply outputString;
    

    Some hints:

    • Don't use /free or /end-free the compiler is able to acknowledge tht on its own.
    • Don't use fixed format declarations to define your fields. They are harder to read and there is no actual reason to use them.