Board index » delphi » Displaying real numbers

Displaying real numbers

How do you truncate/round a real number (i.e., to two decimal places)?
There has to be a simple answer to this question but I can't seem to find
it.

Thanks,
Dave
da...@caps.com

 

Re:Displaying real numbers


Quote
> How do you truncate/round a real number (i.e., to two decimal places)?
> There has to be a simple answer to this question but I can't seem to find
> it.

> Thanks,
> Dave
> da...@caps.com

If you want to round(truncate) the *value* to two decimals, then use
  x := Round(x * 100)/100;
or
  x := Trunc(x * 100)/100;

If you want to *display* the number to two decimal places, use
  Label1.Caption := FloatToStrF(x, ffFixed, 7, 2);
or
  Label1.Caption := Format('%7.2f', [x]);
or
  Label1.Caption := FloatFormat('0.00', x);

Look up these format routines in on-line help for more info.

Greg Mockler
Pietermaritzburg, South Africa
g...@innovative.co.za

Re:Displaying real numbers


In article <01bbc9bd$3eb56da0$a5100...@kimw.caps.com>,
   "Dave Ache" <d...@caps.com> wrote:
]-How do you truncate/round a real number (i.e., to two decimal places)?
]-There has to be a simple answer to this question but I can't seem to find
]-it.

this is for display purposes right?  check the online help topic
"floating point conversion routines".  I find FloatToStrF works
pretty well.  and of course there's always the old Turbo way :
str(MyNum:0:2)

Mark Vaughan

]-
]-Thanks,
]-Dave
]-da...@caps.com

Re:Displaying real numbers


NewNumber := Trunc( OldNumber * 100 ) / 100;
--

Jim Scofield
jscofi...@mindspring.com

Dave Ache <d...@caps.com> wrote in article
<01bbc9bd$3eb56da0$a5100...@kimw.caps.com>...

Quote
> How do you truncate/round a real number (i.e., to two decimal places)?
> There has to be a simple answer to this question but I can't seem to find
> it.

> Thanks,
> Dave
> da...@caps.com

Re:Displaying real numbers


Quote
Dave Ache (d...@caps.com) wrote:

: How do you truncate/round a real number (i.e., to two decimal places)?
: There has to be a simple answer to this question but I can't seem to find
: it.

Try the Format function.

---------------------------------------------------------------
 John English              | mailto:j...@brighton.ac.uk
 Senior Lecturer           | http://www.comp.it.bton.ac.uk/je
 Dept. of Computing        | fax: (+44) 1273 642405
 University of Brighton    |
---------------------------------------------------------------

Re:Displaying real numbers


try this (or similar)

X := round(X*100) / 100;   { rounded to nearest 100th }

--

 Warren Young
The University of Edinburgh

Re:Displaying real numbers


from Delphi Help for "Format":

Unit

SysUtils

Declaration

function Format(const Format: string; const Args: array of const): string;

Description

This function formats the series of arguments in the open array Args.
Formatting is controlled by the Object Pascal format string Format; the
results are returned in the function result as a Pascal string.  
For information on the format strings, see Format Strings.

if this is for display like your header says

Dan Doxtader

Dave Ache <d...@caps.com> wrote in article
<01bbc9bd$3eb56da0$a5100...@kimw.caps.com>...

Quote
> How do you truncate/round a real number (i.e., to two decimal places)?
> There has to be a simple answer to this question but I can't seem to find
> it.

> Thanks,
> Dave
> da...@caps.com

Re:Displaying real numbers


Just one caution:

If you use Trunc you *might* not get the result you want:

From the Delphi Help for Round, with Trunc added by me:

procedure TForm1.Button1Click(Sender: TObject);
var
   S, T: string;
begin
   Str(1.4:2:1, T);
   S := T + ' rounds to ' + IntToStr(Round(1.4)) + #13#10;
   Str(1.5:2:1, T);
   S := S + T + ' rounds to ' + IntToStr(Round(1.5)) + #13#10;
   Str(-1.4:2:1, T);
   S := S + T + ' rounds to ' + IntToStr(Round(-1.4)) + #13#10;
   Str(-1.5:2:1, T);
   S := S + T + ' s to ' + IntToStr(Round(-1.5)) + #13#10;
   Str(1.4:2:1, T);
   S := S + T + ' truncs to ' + IntToStr(trunc(1.4)) + #13#10;
   Str(1.5:2:1, T);
   S := S + T + ' truncs to ' + IntToStr(trunc(1.5)) + #13#10;
   Str(-1.4:2:1, T);
   S := S + T + ' truncs to ' + IntToStr(trunc(-1.4)) + #13#10;
   Str(-1.5:2:1, T);
   S := S + T + ' truncs to ' + IntToStr(trunc(-1.5));
   MessageDlg(S, mtInformation, [mbOk], 0);
end;

Greg Mockler <greg...@iafrica.com> wrote in article
<N.110496.233757...@196-31-97-137.iafrica.com>...

Quote

> > How do you truncate/round a real number (i.e., to two decimal places)?
> > There has to be a simple answer to this question but I can't seem to
find
> > it.

> > Thanks,
> > Dave
> > da...@caps.com

> If you want to round(truncate) the *value* to two decimals, then use
>   x := Round(x * 100)/100;
> or
>   x := Trunc(x * 100)/100;

> If you want to *display* the number to two decimal places, use
>   Label1.Caption := FloatToStrF(x, ffFixed, 7, 2);
> or
>   Label1.Caption := Format('%7.2f', [x]);
> or
>   Label1.Caption := FloatFormat('0.00', x);

> Look up these format routines in on-line help for more info.

> Greg Mockler
> Pietermaritzburg, South Africa
> g...@innovative.co.za

Other Threads