# Board index » delphi » Integer to Float function needed

## Integer to Float function needed

On Wed, 27 Aug 1997 01:05:14 GMT, David Irizarry

##### Quote
<xerx...@ix.netcom.com> wrote:
>Greetings,

>     I'm using Delphi 3.0 and am trying to figure out a good way of converting
>an integer into a real (floating point) number.  It's easy enough to convert
>a real number to an integer using the Trunc() function, but is there a function
>in object pascal to do the reverse (i.e.  turn an integer into a real number).

>I could possibly write a home made function to accomplish this, but I already know that
>it would probably be quite inefficient and waste a lot of clock cycles.

>What's the best way of accomplishing this sort of type conversion?

>Dave Irizarry

In most cases, there is no reason to do this at all (the compiler will
automatically convert integers into reals when necessary, i.e.
1) when assigned to a real variable, explicitly (x := i) or implicitly
(as parameters to a function or procedure)
2) when part of a real type expression

The only situation I can come up with where you need to do a int-float
conversion is when there is an integer expression that needs to be
calculated with floating point arithmetic, e.g.

nrSeconds := 3600 * hr + 60 * min + sec

where hr is an integer variable, and nrSeconds a real.

This might cause integer overflow if hr is larger than 8, because all
terms of the expression are calculated as integer, and only after all
additions have been performed, the conversion into real takes place.

The solution in this case, as in most cases, is forcing the expression
into floating point at an earlier stage, by changing it to

nrSeconds := 3600.0 * hr + 60 * min + sec

Sometimes you might have to do this explicitly. In that case you can
force instant conversion into floating point by multiplying a variable
by 1.0, e.g. in the expression

x := i1 + i2

where i1 and i2 are integers containing large values, so when added
together they might be larger than MAXINT, you would have to change
this to

x := 1.0 * i1 + i2

(which might not even waste any clock cycles)

hth
David

------------------
David A. Schweizer

iec ProGAMMA, The Netherlands
d.a.schweizer[OK, i don't want any more spam]gamma.rug.nl
guess where the '@' goes ?

## Re:Integer to Float function needed

Why the multiplication?

NewReal := OldInt; would do the trick to wouldn't it?

Kim Ommundsen
OmniTree Norge ASA
http://www.omnitree.com/
k...@omnitree.no

Ray Andrews wrote in article <34042CFD.7...@bdsnet.com>...>

##### Quote
>newreal := oldint * 1.0;
>--
>      \\\//
>      (o o)                  Ray Andrews

## Re:Integer to Float function needed

Why the multiplication?

NewReal := OldInt; would do the trick to wouldn't it?

Kim Ommundsen
OmniTree Norge ASA
http://www.omnitree.com/
k...@omnitree.no

Ray Andrews wrote in article <34042CFD.7...@bdsnet.com>...>

##### Quote
>newreal := oldint * 1.0;
>--
>      \\\//
>      (o o)                  Ray Andrews

## Re:Integer to Float function needed

##### Quote
> What's the best way of accomplishing this sort of type conversion?

Let the compiler do it for you, Integer to Float conversion is IMPLICIT:

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
f:Extended;
begin
i:=14;
f:=i; //implicit conversion
Button1.caption:=Floattostr(f);
end;

end.