## Dynamically assigning decimal places in RPGLE

315 pts.
Tags:
AS 400
RPGLE
I have a character field of length 100. I need to convert this to decimal but the decimal points are not known beforehand. Example - In first instance, the character may hold value of '12345'. I need to apply 2 decimals on this and reflect as '123.45'. In example 2, i may need to apply 3 decimals and show it as '12.345'. Hence, its not possible for me to define decimal variable beforehand. Any solution to handle this?

Software/Hardware used:
AS400, RPG, CLLE

## Answer Wiki

Thanks. We'll let you know when a new response is added.

Unless you know the number of decimals before hand, there is no way to do it correctly.

If there was a field that had the number of decimals need you could set up a a bunch if tests.
when dec_needed = 2 then a move to a work_2dec.
when dec_needed = 3 then a move to a work_3dec.
when dec_needed = 4 then a move to a work_4dec.
At the end of test, add to you final workfield.
But unless you know the decimals this will not work…

## Discuss This Question: 4 Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
• There's no way to "display" variable numeric decimal positions, so it's irrelevant if it's in RPG.

It won't be done as a numeric operation. It would be done as character substrings and the result will be displayed as a character string. You can either extract substrings and concatenate them together with a '.' character between or build the displayed string character by character.
34,430 pointsBadges:
report
• @TheRealRaven, thanks. That's what I did. (to use subst function to extract portions of string and added . accordingly). Just was wondering if there was an option to display variable decimals.
315 pointsBadges:
report
• @ToddN2000, thanks. That's cumbersome. I used Subst function to extract values accordingly. Just was wondering if there was a better approach.
315 pointsBadges:
report
• If you are trying to display the edited number, then substring as you've done is about as good as it gets. If you are trying to input the 'number' and use it in calcs then I'd suggest %dec to convert and divide by (10 ^ decimals).
2,950 pointsBadges:
report