Crystal Reports将数字转换为文本

时间:2016-03-29 17:33:04

标签: crystal-reports

使用Crystal Reports版本8,我需要将numbervar转换为stringvar。我已经尝试过ToText()(带有大写的所有变体)和CStr()(也有不同的大小写),每次CR告诉我“这里需要一个数字”并将光标移动到我的else块的开头。最终尝试将存储为NumberVars的小时和分钟转换为字符串,这样我就可以显示“8h 30m”而不是8.50。

到目前为止,这是我对公式的看法:

if {Collect2000Log.LogCode} = "0002" then 0
else 
(
NumberVar OldTime := ((DateDiff("n",{@NextTime},{Collect2000Log.LogWhen})/60)*-1);

NumberVar Hours;
NumberVar Minutes;
StringVar strHours;
StringVar strMinutes;
StringVar NewTime;
//Extract the number of hours
Hours := Int(OldTime);
//Get the decimal portion for minutes
Minutes := Remainder(OldTime, 1) * 100;
//Divide the minutes by 60 to increase the number of hours
Hours := Hours + Int(Minutes / 60);
//Get the remainder for the number of minutes left over
Minutes := Remainder(Minutes, 60);
//Convert hours & mins to strings
strHours := ToText(Hours);
strMinutes := ToText(Minutes):
NewTime := strHours & "h " & strMinutes & "m";
);

现在,当我添加这个公式时,CR说“结束了”,“我很难过。”我能够绕过那一次,但现在看起来并不那么充满希望。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

问题很简单......在IFElse中,您需要返回相同的数据类型,但是您要通过IF返回一个数字,并通过Else返回一个字符串因此在else块开始时出现错误a Number is required ..所以将If的输出转换为如下所示的字符串。

if {Collect2000Log.LogCode} = "0002" 
then ToText(0)
else 
(
NumberVar OldTime := ((DateDiff("n",{@NextTime},{Collect2000Log.LogWhen})/60)*-1);

NumberVar Hours;
NumberVar Minutes;
StringVar strHours;
StringVar strMinutes;
StringVar NewTime;
//Extract the number of hours
Hours := Int(OldTime);
//Get the decimal portion for minutes
Minutes := Remainder(OldTime, 1) * 100;
//Divide the minutes by 60 to increase the number of hours
Hours := Hours + Int(Minutes / 60);
//Get the remainder for the number of minutes left over
Minutes := Remainder(Minutes, 60);
//Convert hours & mins to strings
strHours := ToText(Hours);
strMinutes := ToText(Minutes):
NewTime := strHours & "h " & strMinutes & "m";
);