【发布时间】:2012-09-19 10:38:14
【问题描述】:
考虑:
function x_StrZero(N: Double; W: Integer; D: Integer = 0): String;
var S : String;
begin
Str(N:W:D,S);
S := Trim(S);
这将 W1057 隐式字符串从“ShortString”转换为“字符串”
online doc 说:
procedure Str(const X [: Width [:Decimals]]; var S: String);
还有
注意:但是,在使用此过程时,编译器可能会发出警告:W1057 Implicit string cast from '%s' to '%s' (Delphi)。
为什么会这样?
我想阻止这种丑陋的解决方法:
function x_StrZero(N: Double; W: Integer; D: Integer = 0): String;
var
S : String;
SS : ShortString;
begin
Str(N:W:D,SS);
S := Trim(String(SS));
我已阅读 Why does Delphi warn when assigning ShortString to string? 但这并没有回答这个问题。
【问题讨论】:
-
如果这种解决方法对你来说很难看,你可以使用 format
-
只是一个疯狂的猜测,你试过 S:Ansistring 和 {$O-} 吗? Str 是 TurboPascal 的古老函数,可能是 TP 字符串上的 asm 实现。虽然 IntToStr 缺少该功能,但我还是会避免使用那些古老的程序。
-
TJclNumericFormat 和函数 IntToStrZeroPad(Value, Count: Integer): string;也可能是可行的替代品
-
正确的做法是停止使用
Str -
我同意你的观点,它是古代码
标签: delphi unicode delphi-xe2