Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use TExtendedHelper on literals?

With System.SysUtils.TShortIntHelper (and others) I can write:

output := 5.ToString();

to format the number 5 as string. As well, there is System.SysUtls.TExtendedHelper, but I'm unable to compile:

output := (5.0).ToString();

E2018: Record, object or class type required

Other versions which don't work:

  • 5.0.ToString()
  • (1.0+5.1).toString()
  • (5+0.).toString() (says E2029: ')' expected but ']' found)

Versions which do actually work:

  • (1+5.1).toString()
  • (1.1+1+5.1).toString()
  • 5.9e0.toString()

If the extended value is declared const, it doesn't work either:

function TestFormat(): String;
const
  q = 5.5;
begin
  Result := q.ToString();
end;

But with a definition of q : extended = 5.5; it works. So, I'm wondering why the compiler behaves this way.

like image 577
ventiseis Avatar asked Mar 18 '17 23:03

ventiseis


1 Answers

You found errors in the compiler. Please report it in Quality Portal.

A workaround is to use the helpers class functions:

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;
like image 181
LU RD Avatar answered Nov 08 '22 08:11

LU RD