我处于需要计算变量的前导0并使用4GL代码将它们放在另一个变量中的情况。
例如,如果变量i(整数)是'00000546'那么
请告诉我。
答案 0 :(得分:2)
计算前导零的最简单方法:
DEF VAR iNum AS INTEGER NO-UNDO.
DEF VAR cValue AS CHAR NO-UNDO.
DEF VAR iLeadZero AS INTEGER NO-UNDO.
ASSIGN
cValue = '00000546'
iNum = INTEGER(cValue)
iLeadZero = LENGTH(cValue) - LENGTH(STRING(iNum)).
也可以作为一个函数完成(没有错误处理):
FUNCTION cntLeadZeros RETURNS INTEGER
(INPUT pcValue AS CHARACTER):
DEF VAR iNum AS INTEGER NO-UNDO.
DEF VAR iLeadZero AS INTEGER NO-UNDO.
ASSIGN
iNum = INTEGER(pcValue)
iLeadZero = LENGTH(pcValue) - LENGTH(string(iNum)).
RETURN iLeadZero.
END FUNCTION. /* cntLeadZeros */
具有错误处理功能,因此如果人们通过非整数值。
FUNCTION cntLeadZeros2 RETURNS INTEGER
(INPUT pcValue AS CHARACTER):
DEF VAR iNum AS INTEGER NO-UNDO.
DEF VAR iLeadZero AS INTEGER NO-UNDO.
ASSIGN
iNum = INTEGER(pcValue)
iLeadZero = LENGTH(pcValue) - LENGTH(string(iNum)).
IF ERROR-STATUS:ERROR THEN
RETURN ?.
RETURN iLeadZero.
END FUNCTION. /* cntLeadZeros2 */
答案 1 :(得分:1)
好的,所以我是一个整数。需要确定i的格式,用于创建默认字符串,然后将其拆分为零和非零。
这个怎么样:
DEFINE VARIABLE i AS INTEGER NO-UNDO
FORMAT "9999999999".
DEFINE VARIABLE vcINoLeading AS CHARACTER NO-UNDO.
DEFINE VARIABLE vcIDefault AS CHARACTER NO-UNDO.
DEFINE VARIABLE vcIZeros AS CHARACTER NO-UNDO.
DEFINE FRAME bogus
i.
i = 1234.
vcIDefault = STRING(i,i:FORMAT).
vcINoLeading = STRING(i).
vciZeros = REPLACE(vcIDefault,vcINoLeading,"").
MESSAGE vcIDefault SKIP vcINoLeading SKIP vciZeros VIEW-AS ALERT-BOX.
vcINoLeading只有非零数字,vciZeros全部为零。
答案 2 :(得分:0)
这是一种可以完成的方式,遍历字符:
def var a as char no-undo.
def var b as char no-undo.
def var c as char no-undo.
def var v as int no-undo.
assign a = '00000546'.
do v = 1 to length(a):
assign c = substring(a,v,1).
if c ne "0" then leave.
end.
assign b = substring(a,v).
disp fill("0", v - 1) b.
答案 3 :(得分:0)
问题中唯一棘手的部分是他提到“i”是一个整数。因此,前导零必须来自“i”的格式,而不是作为字符串的一部分。值得庆幸的是,我们可以检查“i”的格式并使用它来确定显示的前导零数量。
DEFINE VARIABLE i AS INTEGER FORMAT '9999999999' NO-UNDO.
DEFINE VARIABLE cZeros AS CHARACTER FORMAT 'X(9)' NO-UNDO.
DEFINE FRAME iFrame
i.
i = 6123455.
cZeros = FILL('0',LENGTH(i:FORMAT) - LENGTH(STRING(i))).
DISPLAY
i
cZeros
WITH FRAME iFrame.
答案 4 :(得分:0)
我不明白你为什么要像你描述的那样组装新变量,但你可能有充分的理由。也许你可以解释更多?
无论如何,我应该这样做:
DEFINE VARIABLE a AS CHARACTER NO-UNDO.
DEFINE VARIABLE nr AS INTEGER NO-UNDO.
DEFINE VARIABLE b AS CHARACTER NO-UNDO.
a = STRING(546,"99999999").
nr = LENGTH(a) - LENGTH(LEFT-TRIM(a,"0")).
b = FILL("0",nr) + LEFT-TRIM(a,"0").
MESSAGE a SKIP nr SKIP b
VIEW-AS ALERT-BOX INFO BUTTONS OK.
答案 5 :(得分:0)
整数数据类型没有前导零,因此您不需要计数零。您可以使用STRING函数执行此操作,如下例所示。在下面的这个例子中,我正在考虑你的字符串有一个固定的长度。
DEFINE VARIABLE i-test AS INTEGER INITIAL 546.
DEFINE VARIABLE c-test AS CHARACTER.
ASSIGN c-test = STRING(i-test,"99999999").
MESSAGE c-test VIEW-AS ALERT-BOX.
消息将返回:00000546