Solved

Real Value to Integer.....

Posted on 1998-09-29
4
1,395 Views
Last Modified: 2008-03-10
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
Comment
Question by:lar_jens
  • 2
4 Comments
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
You have to round the float to get the integer:

i := Round(e);

Cheers,
JB
0
 

Accepted Solution

by:
gaona earned 100 total points
Comment Utility
You have to Trunc the float to get the integer:

i := Trunc(e);

Regards,
Gaona

0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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

by:gaona
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now