Numeric target too small when using %DEC

Q. I guess I’ve misinterpreted how %DEC is supposed to work. In the following example, the value in Char4 is 0250, so I was anticipating the same result as a Move operation — with my numeric result as 4 long, 3 decimal positions. But I get the error “The target for a numeric operation is too small to hold the result.”

Num43 = %Dec(Char4:4:3); 

Do you have a simple solution?

A. The %DEC function converts a character value to a packed decimal value, but the decimal does not align itself the same way that the Move operation did. The decimal point in the character field is optional, but if it’s not there, %DEC will assume you are converting to a whole numeric value, without decimals. In your example, %DEC is trying to convert 0250, not 0.250; ergo, the “too small to hold result” error.

If you want to align the decimals with the attributes of the result, the generic formula is:

NumResult = %Dec(CharSource:%Size(CharSource):0) /(10**%Decpos(NumResult)); 

Or, in your example,

Num43 = %Dec(Char4:4:0)/1000; 

The RPG IV Reference isn’t very clear on this, so your confusion is understandable.