How to convert a integer to float in Delphi?

57,587

Solution 1

i*1.0 should convert it to a floatingpoint number. Any calculation involving floatingpoint numbers of any type gets implicitly converted to extendend and then implicitly converted to the desired result type on assignment. In contrast to C/C++ all calculations happen in Extended(80 bit float, the internal format of the x87 floatingpoint unit) and are converted back later.

real(i) might work too.

Solution 2

Integer to Float

There is not need to cast anything, just assign

Float1 := Integer1;

Your question seem be Float to Integer

Two options

Integer1 := Trunc(Float1); //truncate 

or

Integer1 := Round(Float1); //Round

Solution 3

You can do:

myFloat := myInteger;

Solution 4

3 possible ways, depending on the kind of expressions you have.

var
  Float: Double;
  Int1: Integer;
  Int2: Integer;
begin
  Int1 := 925;
  Int2 := 21;
  Float := Int1; // simple form: assign it
  Writeln(Float);
  Float := Int2 / Int1; // floating point division: assign it
  Writeln(Float);
  Float := (1.0 * Int2) * Int1; // integer operator: multiply by 1.0 and add parenthesis
  Writeln(Float);
end.

Output:

 9.25000000000000E+0002
 2.27027027027027E-0002
 1.94250000000000E+0004

Solution 5

I have this two (i have more, but for example would be enough) oveloaded functions:

interface
   function MyFunc(Value:Integer):Integer;overload;
   function MyFunc(Value:Double):Double;overload;
implementation
   function MyFunc(Value:Integer):Integer;
   begin
        MyFunc:=Math.Ceil( {Do some extra complicated Math calcs}
                          *
                           MyFunc( {¿How to Type Cast as Double?} Value )
                         );
   end; 
   function MyFunc(Value:Double):Double;
   begin
        MyFunc:={Do some Math calcs with Value};
   end; 

How can in Typecast Integer onto Double? I hate putting ´(1.0*Value)´ or ´(0.0+Value)´ that uses less CPU time, must exist some other way to do it.

BEWARE: i mean typecast on that call!!! BEWARE: the call is on an overloaded function, one calls the other

Hope someone find a way, cause putting ´MyFunc((Double)Value)´ says invalid typecast... why on the hell casting an integer to a double is not possible???? a double can store an integer... also more, when assigning it cast it directly... ´MyVarOfTypeDouble:=MyvarOfTypeInteger;´ works perfect.

The problem comes with overloaded functions... it does not know i want to call to Double version one... that is why a cast is needed...

In other words... this will work like a charm:

interface
   function MyFunc_Integer(Value:Integer):Integer;
   function MyFunc_Double(Value:Double):Double;
implementation
   function MyFunc_Integer(Value:Integer):Integer;
   begin
        MyFunc_Integer:=Math.Ceil( {Do some extra complicated Math calcs}
                                  *
                                   MyFunc_Double(Value)
                                  );
   end; 
   function MyFunc_Double(Value:Double):Double;
   begin
        MyFunc_Double:={Do some Math calcs with Value};
   end; 

See, if functions names are different, no need to cast... till i found a better solution i must be afraid that i will use ´(0.0+Value)´ where it should work (but does not) ´(Double)value´, whe Value is of type Integer.

So my response is... instead of putting code ´(Double)SomethingOfTypeInteger´ (that should work but complier does not like it) ... put this other code ´(0.0+SomethingOfTypeInteger)´, i do recomend not use ´(1.0*SomethingOfTypeInteger)´ since it is much less efficient...

Never ever think on creating a function like this one (it is also worst than 1.0*Value):

function ConvetIntegerToDouble(Value:Integer):Double;
begin
     ConvetIntegerToDouble:=Value;
end;

That function complies great... but takes a lot, lot more time than putting ´0.0+value´.

Now to Very, Very... a lot very Intelligent people... see this (very ugly) code:

    interface
       function MyFunc(Value:Integer):Integer;overload;
       function MyFunc(Value:Real48):Real48;overload;
       function MyFunc(Value:Real):Real;overload;
       function MyFunc(Value:Double):Double;overload;
       procedure CanYouGuess;
    implementation
    var
       MyVarOfTypeDouble:Double;
       function MyFunc(Value:Integer):Integer;
       begin
            MyFunc:=Round(MyFunc(1.0+Value));
       end;
       function MyFunc(Value:Real48):Real48;
       begin
            MyFunc:=MyFunc(2.0+Value);
       end;
       function MyFunc(Value:Real):Real;
       begin
            MyFunc:=MyFunc(4.0+Value);
       end;
       function MyFunc(Value:Double):Double;
       begin
            MyFunc:=(8.0+Value);
       end;
       procedure CanYouGuess;
       var
          MyVarOfTypeReal48:Real48;
          MyVarOfTypeReal:Real;
       begin
            MyVarOfTypeDouble:=MyFunc( 0 ); // What value will be on MyVarOfTypeDouble?
            MyVarOfTypeReal48:=0;
            MyVarOfTypeDouble:=MyFunc(MyVarOfTypeReal48); // What value will be on MyVarOfTypeDouble?
            MyVarOfTypeReal:=0;
            MyVarOfTypeDouble:=MyFunc(MyVarOfTypeReal); // What value will be on MyVarOfTypeDouble?
            MyVarOfTypeDouble:=MyFunc(0.0); // What value will be on MyVarOfTypeDouble?
       end;

Now try to guess values... reading code it is not clear what calls must go... i pass an integer, then inside Integer oveload version... which one is called... Real48 overloaded or Real one or Double one? ... same applies to passing a Real48 and also passing a Real...

Compiler found no error at all... but i do not know which (if only one) will be called... without tracing, of course!!! can you?

For such situations is where Explicit cast would be great to work...

Please note i put 1.0+, 2.0+, 4.0+, 8.0+ ... powers of 2... so any sum of any or some of them can be seen... if resoult is 15... all of them had be run... if 3 only 1.0 and 2.0, etc... binary mode!!! like bits weight, etc... it is just to show how a code can get too complicated... just because an overloaded function must have different params...

This is not allowed, since compiler does not know which one to call:

    interface
       function MyFunc(Value:Double):Integer;overload;
       function MyFunc(Value:Double):Real48;overload;
       function MyFunc(Value:Double):Real;overload;
       function MyFunc(Value:Double):Double;overload;

Yes, compiler is a quite stupid... it can be compiled only in cases where MyFunc is used on an assigment.... se this code:

MyVarOfTypeReal48:=MyFunc(1); // It can know return value must be Real48, so call that one... but compiler is not so Intelligent

In such cases it can be known which one to call.... the problem is with:

MyVarOfTypeInteger:=Round(5*MyFunc(1)+MyFunc(2)*1.3); // This is problematic, first one could be understod as Integer version, second one as ... as what? Real, Real48 or Double? not possible to know... that is why compiler does not like such overloaded (with same arguments).

Hope this helps!

Share:
57,587
Aftershock
Author by

Aftershock

Updated on January 18, 2020

Comments

  • Aftershock
    Aftershock over 4 years

    How to convert a integer to float in Delphi?

    E.g int_h:= int_var/1.5* int_var;

  • Aftershock
    Aftershock over 13 years
    I would like it in the other way round.
  • Barry Kelly
    Barry Kelly over 13 years
    That typecast is not valid: E2089 Invalid typecast.
  • Trinidad
    Trinidad over 13 years
    The opposite of Trunc() is actually Ceil(). Trunc() will round towards zero, Ceil() will round either towards +Infinity (if it is a positive number) or -Infinity (if negative).
  • CodesInChaos
    CodesInChaos over 13 years
    No Ceil will always round to +inf, Floor will always round to -inf, and Trunc always to 0. So the opposite of Ceil() isn't Trunc, but Floor.
  • Jeroen Wiert Pluimers
    Jeroen Wiert Pluimers over 13 years
    First a +1, then a -1; I'd love motivation for both: it helps me write better answers.
  • Reversed Engineer
    Reversed Engineer about 5 years
    I'd rather say i + 0.0 instead of i * 1.0, since even those in grade 1 can add, but I think only in grade 2 you learn to multiply. (Just kidding - multiplication needs much more CPU or FPU power than addition)