Hi, How to convert a integer to float in Delphi?
E.g int_h:= int_var/1.5* int_var;
Hi, How to convert a integer to float in Delphi?
E.g int_h:= int_var/1.5* int_var;
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.
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
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