delphi - 如何在文字上使用 TExtendedHelper?

标签 delphi delphi-10.1-berlin class-helpers

使用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/

相关文章:

delphi - 开发新代码时是否应该使用类助手?

delphi - 如何向 TTabSheet 添加属性,以便在设计时与 TPageControl 一起使用

delphi - 使用 RTTI 在运行时查找 Delphi 中的所有类助手?

delphi - 使用位图时出现 "Not enough storage"错误

delphi - 使用 Delphi 获取真实硬盘序列号

android - 为什么 Android FireMonkey 应用程序中的控件不能跨越多列或多行?

rest - 如何模拟超时属性?

Delphi 7 - 将 Base 64 字符串解码到文件

delphi - 为什么我不能将过程变量与 nil 进行比较?

delphi - 如何获取(方法: TRttiMethod) in TVirtualInterface TVirtualInterfaceInvokeEvent?