x
Solved

# Real Value to Integer.....

Posted on 1998-09-29
Medium Priority
1,413 Views
Hey folks..

I need to know how to cast a floating point value into an
integer..

Suppose I have declared two variables..

i: Integer;
e: Extended;

Then

i := e;              //Doesn't work
i := Int(e);         //Doesn't work
i := FloatToInt(e);  //Doesn't work
i := Integer(e);     //Doesn't work

What do I do to make this work??
0
Question by:lar_jens
• 2

LVL 5

Expert Comment

ID: 1341118
You have to round the float to get the integer:

i := Round(e);

Cheers,
JB
0

Accepted Solution

gaona earned 200 total points
ID: 1341119
You have to Trunc the float to get the integer:

i := Trunc(e);

Regards,
Gaona

0

LVL 4

Expert Comment

ID: 1341120
Hah!
You're wrong all of you! Here's the real way to do it:

i := StrToInt( Copy( Format( '%g', [ e ] ), 1, Pos( DecimalSeparator, Format( '%g', [ e ] ) ) - 1 ) ) +
StrToInt( Copy( Format( '%g', [ Frac( e ) + 0.5 ] ), Pos( DecimalSeparator, Format( '%g', [ Frac( e ) + 0.5 ] ) ) - 1, 1 ) );

;->

BTW, use JimBob's solution. Never use Trunc unless it is exlplicitly stated that all decimals should be neglected.

/// John
0

Expert Comment

ID: 1341121
If you want to round the value of the variable ' e' for the whole closer, discarding its decimals, use the function:

i:=trunc(e+0.5)

that substitutes the function i:=round(e).

thank you for the points.

gaona
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.