Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Crystal reports 如何在crystal reports中向文本字段添加前导零_Crystal Reports_Formula_Leading Zero_Crystal Reports Formulas - Fatal编程技术网

Crystal reports 如何在crystal reports中向文本字段添加前导零

Crystal reports 如何在crystal reports中向文本字段添加前导零,crystal-reports,formula,leading-zero,crystal-reports-formulas,Crystal Reports,Formula,Leading Zero,Crystal Reports Formulas,我在想一种方法,将前导零添加到字符串字段值中。 例如,我有12345,但需要一个将其转换为012345的公式。我对Crystal是新手,因为我知道这可能是一个简单的公式,但似乎无法让它起作用 12345 => 012345 (add leading zero to make it 6 chars) 提前谢谢 只需在字段“0”+yourstringchar中添加一个数字字符串值,并将其填充到特定长度: local numbervar yournum := tonumber({table.

我在想一种方法,将前导零添加到字符串字段值中。 例如,我有12345,但需要一个将其转换为012345的公式。我对Crystal是新手,因为我知道这可能是一个简单的公式,但似乎无法让它起作用

12345 => 012345  (add leading zero to make it 6 chars)

提前谢谢

只需在字段“0”+yourstringchar

中添加一个数字字符串值,并将其填充到特定长度:

local numbervar yournum := tonumber({table.your_string}); //convert to number
totext(yournumnum, '000000') //convert back to padded string of length 6
还是通用字符串

local stringvar yourstring:= {table.your_string};
local numbervar LENGTH := 10; //The desired padded string length

if length(yourstring) >= LENGTH then yourstring else
  replicatestring('0',LENGTH-length(yourstring)) + yourstring

上面的逻辑工作,它的一个通用公式左填充

如果你想要正确的填充,使用这个

  local stringvar yourstring:= {table.your_string};
  local numbervar LENGTH := 10; //The desired padded string length

  if length(yourstring) >= LENGTH 
        then yourstring 
  else
        yourstring + replicatestring('0',LENGTH-length(yourstring)) 
--坎蒂

试试这个

totext(您的_编号,“000000”)

第一个参数:嗯,这是输入

第二个参数:输出中需要的位数

例如

num=1234

totext(num,“000000”)

输出:

001234

而且,如果要添加固定数量的零,则可以使用(添加3个前导零):

“000”+totext(您的_编号,0);//添加3个前导零的步骤

注意:最终输出是一个字符串而不是一个数字。我使用这个:

Right("00000000"&ToText({Table.correla}),8)
replace((space((20)-len({Table.Field}))+({Table.Field}))," ","0")
基本上,它生成前导空格,然后用零替换这些空格。通过从添加的空格中减去字段长度,字段将始终是空格((XX)中指定的字符数


它看起来过于复杂,但实际上简化了需要大量固定长度字段的报表。在开发报表时,我可以将此代码复制粘贴到新公式中,更改空格数以匹配所需字段长度,然后更改我的字段名。

如何在crystal报表中使用公式, 要在下面输入公式(打开(字段浏览器)->然后右键单击(公式字段)->选择新建-->创建公式名称(xyz)->然后输入 **ToText({DataTable1.Sr_No},“0000”)**

在**之间复制/仅复制粗体文本,然后保存并关闭

在将公式添加到crystal report并运行之后,您需要的答案就在那里了

没有。 0001 0002
0003

值是否总是相同的长度,或者您是否需要用零填充一个数字到一定数量的位置?例如12345变为
012345
,2变为
000002
。我使用了Ryan的答案,但有人知道是否有更简单的方法可以做到这一点,而无需创建新的公式吗?事实上有一个设置数字字段的格式设置为“前导零”,但勾选它没有任何作用。似乎大多数找到这个答案的人也可以使用这个信息。