35 pts.
 How to capture decimal value of numeric fld
RPG
I need a way to capture the decimal value of a numeric field (i.e. 5.78). Need a way to be able to break the decimal value away to do computations on it. How do I achieve this using RPG III coding techniques?

Software/Hardware used:
AS/400 I series
ASKED: October 18, 2012  6:50 PM

Answer Wiki:
What do you need to do with it? That may determine the best technique to use. One way would be to use a data structure, another would be to z-add it to a field with with length/dec positions.
Last Wiki Answer Submitted:  October 18, 2012  7:14 pm  by  CharlieBrowne   32,855 pts.
All Answer Wiki Contributors:  CharlieBrowne   32,855 pts. , Tom1960   35 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

It depends on exact circumstances, but using data structure subfields will get your exact requirement handled.
  I DS
I 1 53@CVT
I 3 53@FRAC
I DS
I 1 33FRAC
C Z-ADD5.78 @CVT
C Z-ADD@FRAC FRAC
C*** DO CALCS WITH FRAC…
C SETON LR
 Define the subfields to hold the largest numbers of digits, including fractional digits, that your program needs to handle. Make sure that the two intermediate subfields (@CVT and @FRAC) are defined as zoned decimal.
 
(We’ll see how the “code” looks after it gets posted.)
 
Tom

 108,055 pts.

 

Well, sorry about the formatting. This editor makes very little sense.
 
The @CVT field is defined over positions 1-5 with 3 decimal fraction positions. It’s zoned decimal. Both @FRAC and FRAC also have 3 decimal positions and are zoned decimal. @FRAC is defined starting position 3 of the DS because that’s where the fractional portion of @CVT begins. @CVT is where your numeric value will be placed for conversion to just the fractional portion.
 
Tom

 108,055 pts.

 

Or perhaps an EVAL statement like:DecVal = Val – %int(Val)

 44,180 pts.

 

Oh sorry … RPG III .. why would you do that?  Is this another interview question? .  But if you must, check out the DIV and MVR commands.  If you divide by 1 the Remainder (MVR) would be the decimal. 

 44,180 pts.