What is the best way to convert an integer to a floating point value without assigning a variable?

I would like to know how to convert from an integer to a floating point value without assigning an intermediate variable. This code is as follows:

Format('Theoretical peak scaling %6.2f', [ThreadCount])

This is not explicitly executed at run time because it ThreadCountis an integer.

I tried the obvious

Format('Theoretical peak scaling %6.2f', [Double(ThreadCount)])

and the compiler rejects this with

E2089 Invalid typecast

I know I can write

Format('Theoretical peak scaling %6.2f', [ThreadCount*1.0])

but it is poorly read and will simply tempt the future maintainer to remove the multiplication error.

Does anyone know of a clean way to do this without an intermediate variable, and in a way that makes codes clear for future readers?

+5
source share
5 answers

, * 1.0,

Format('Theoretical peak scaling %6.2f', [Double(Variant(ThreadCount))])
+4

... , :

Format('Theoretical peak scaling %3d.00', [ThreadCount]);

, - , ?

+8

record helper :

type
  TIntHelper = record helper for Integer
    function AsFloat : Double;
  end;

function TIntHelper.AsFloat: Double;
begin
  Result := Self;
end;

Format('Theoretical peak scaling %6.2f', [ThreadCount.AsFloat])

XE3, Embarcadero. , Emarcadero , RTL.

Marco Cantu:

  • ( , )

  • , , , . , , .

: On Record/Class/Type Helpers.

: XE4, , TIntegerHelper, ToDouble.


RTTI, :

Format('Theoretical peak scaling %6.2f', 
  [TValue.From<Integer>(ThreadCount).AsExtended])

FTR, , Double(Variant(i)) i.AsFloat , TValue.From<Integer>(i).AsExtended - 200+ .

+5

:

function IntToDouble(const AInt: Integer): Double;
begin
  Result := AInt;
end;


Format('Theoretical peak scaling %6.2f', [IntToDouble(ThreadCount)]);
+3

- :

Format('Theoretical peak scaling %3d.00', [ThreadCount])

ThreadCount , .; >

+3

All Articles