Dag*_*200 5 formula crystal-reports leading-zero crystal-reports-formulas
我试图想办法将一个前导零添加到字符串字段值.例如,我有12345,但需要一个公式,将其转换为012345.我是Crystal的新手,因为我知道这可能是一个简单的公式,但似乎无法让它工作.
12345 => 012345 (add leading zero to make it 6 chars)
Run Code Online (Sandbox Code Playgroud)
提前致谢.
用零填充数字字符串值到特定长度:
local numbervar yournum := tonumber({table.your_string}); //convert to number
totext(yournumnum, '000000') //convert back to padded string of length 6Run Code Online (Sandbox Code Playgroud)
OR 用于通用字符串
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
Run Code Online (Sandbox Code Playgroud)
试试这个
totext(your_number,"000000");
1st arg.:嗯,这是输入.
2nd arg.:输出中需要的位数.
对于前者,
num = 1234;
totext(num,"000000");
输出:
001234
而且,如果你想添加固定数量的零,那么你可以使用(添加3个前导零):
"000"+ totext(your_number,0); //添加3个前导零
注意:最终输出是字符串而不是数字.