Solved

Real Value to Integer.....

Posted on 1998-09-29
4
1,403 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
ID: 1341118
You have to round the float to get the integer:

i := Round(e);

Cheers,
JB
0
 

Accepted Solution

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

i := Trunc(e);

Regards,
Gaona

0
 
LVL 4

Expert Comment

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reconfigure Delphi Install? 2 65
Convert MS Word document to a PDF file 9 114
how to change, disabled button color FMX ? 1 49
Broadcast a message using ICS 2 22
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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