如何在文字上使用TExtendedHelper?

ven*_*eis 5 delphi class-helpers delphi-10.1-berlin

System.SysUtils.TShortIntHelper(和其他人)我可以写:

output := 5.ToString();
Run Code Online (Sandbox Code Playgroud)

将数字格式化5string.同样,有System.SysUtls.TExtendedHelper,但我无法编译:

output := (5.0).ToString();
Run Code Online (Sandbox Code Playgroud)

E2018:需要记录,对象或类类型

其他不起作用的版本:

  • 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;
Run Code Online (Sandbox Code Playgroud)

但它的定义是q : extended = 5.5;有效的.所以,我想知道为什么编译器会以这种方式运行.

LU *_* RD 6

您在编译器中发现了错误.请在Quality Portal中报告.

解决方法是使用helpers类函数:

myString := Extended.ToString(5.5);
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)

  • 提交了[错误报告,发布RSP-17093](https://quality.embarcadero.com/browse/RSP-17093). (2认同)