Search & Replace Last Digit

SKaushal

New Member
Hi Everybody,

Problem: I have to change the last digit of a negative number as follows:

if a negative number ends in "0" then change last digit to "}"
if a negative number ends in "1" then change last digit to "K"
and so on..................

Help me out Please.

Thanx
 
DEF VAR a AS INT FORMAT "->>>>>9".
UPDATE a.
IF a < 0 THEN DO:
CASE -(a) MOD 10 :
WHEN 0 THEN MESSAGE SUBSTRING(STRING(a),1,LENGTH(STRING(a)) - 1 ) + "}".
OTHERWISE MESSAGE SUBSTRING(STRING(a),1,LENGTH(STRING(a)) - 1 ) + entry( -(a) MOD 10, "K,L,M,N").
END CASE.
END.
 
How about this one?

Code:
def var lc_answer as char no-undo.
def var lf_val as decimal no-undo.
def var li_len as int no-undo.

if lf_val < 0
then assign
    lc_answer = string(-1.0 * lf_val)
    li_len = length(lc_answer)
    substring(lc_answer, li_len, 1) = 
        entry(int(substring(lc_answer, li_len, 1)) + 1, "},K,L,M,N,O,P,Q,R,S").
else lc_answer = string(lf_val)
 
I think if you are using the decimal datatype, you should consider the format of the variable.

The attachment example is applicable for last digit of format of decimal data type . example :
a = -12.345 ( format "->>>>9.999" ) the last digit value = 5.
a = -12.3450 ( format "->>>>9.9999" ) the last digit value = 0.

Please see the example in attachment file.
 

Attachments

If format is an issue here is a version that works with various formats.
Code:
function NumFormat returns char (ipf_val as decimal, ipc_format as char):
   def var lc_answer as char no-undo.
   def var li_len as int no-undo.

   if ipf_val < 0
   then assign
       lc_answer = trim(string(-1.0 * ipf_val, ipc_format))
       li_len = length(lc_answer)
       substring(lc_answer, li_len, 1) =
           entry(int(substring(lc_answer, li_len, 1)) + 1, "},K,L,M,N,O,P,Q,R,S").
   else lc_answer = trim(string(ipf_val, ipc_format)).

   return lc_answer.

end function.
I would say it's more than likley that this format is required for a data-dump type program. A function seems the most useful way to go.
 
Back
Top