I was looking for the best approach to convert .01 or -.01 to 0.01 or -0.01. This is needed when I want to convert the decimal to character.
After searching a lot with EDITW and EDITC I ended up with below code.
D Sales S 9S 2
D Net_Sales S 10A
// Sales variable may contain positive or negative amount
If Sales >= 0
Net_Sales = %Trim(%Xlate(' ':'0': %EditC(Sales:'3'):7)) ;
Else
Net_Sales = '-' + %Trim(%Xlate(' ':'0': %EditC(Sales:'3'):7))
EndIf
This works fine for all case, but some how I was not convinced. And accidentally I have noticed a header specification DECEDIT
H DECEDIT('0.')
will this be my best alternative for the requirement or do we have anything else?
Thanks in advance for your help!
If you want the zero to print before the decimal, and a floating negative sign, you need to use ctl-opt decedit('0.')
. Here are some samples comparing %char()
, %editc()
, and %editw()
:
dcl-s value1 Zoned(7:2) Inz(.01);
dcl-s value2 Zoned(7:2) Inz(-.01);
dcl-s output Char(10) Inz('');
output = %char(value1);
dsply output;
output = %char(value2);
dsply output;
output = %editc(value1:'N');
dsply output;
output = %editc(value2:'N');
dsply output;
output = %editw(value1:' , 0 . -');
dsply output;
output = %editw(value2:' , 0 . -');
dsply output;
return;
When compiled with ctl-opt decedit('0.');
the output is:
DSPLY 0.01
DSPLY -0.01
DSPLY 0.01
DSPLY -0.01
DSPLY 0.01
DSPLY 0.01-
When compiled without ctl-opt decedit('0.');
the output is:
DSPLY .01
DSPLY -.01
DSPLY .01
DSPLY -.01
DSPLY 0.01
DSPLY 0.01-