使用System.SysUtils.TShortIntHelper
(和其他)我可以写:
output := 5.ToString();
将数字5
格式化为字符串
。另外,还有 System.SysUtls.TExtendedHelper
,但我无法编译:
output := (5.0).ToString();
E2018: Record, object or class type required
其他不工作的版本:
5.0.ToString()
(1.0+5.1).toString()
(5+0.).toString()
(表示 E2029:预期为“)”,但找到了“]”)
实际工作的版本:
(1+5.1).toString()
(1.1+1+5.1).toString()
5.9e0.toString()
如果扩展值被声明为 const
,它也不起作用:
function TestFormat(): String;
const
q = 5.5;
begin
Result := q.ToString();
end;
但是通过定义 q : Extended = 5.5;
就可以了。所以,我想知道为什么编译器会这样。
最佳答案
您在编译器中发现了错误。请在质量门户中报告。
解决方法是使用辅助类函数:
myString := Extended.ToString(5.5);
class function ToString(const Value: Extended): string; overload; inline; static; class function ToString(const Value: Extended; const AFormatSettings: TFormatSettings): string; overload; inline; static; class function ToString(const Value: Extended; const Format: TFloatFormat; const Precision, Digits: Integer): string; overload; inline; static; class function ToString(const Value: Extended; const Format: TFloatFormat; const Precision, Digits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline; static;
关于delphi - 如何在文字上使用 TExtendedHelper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42881299/