Board index » delphi » Easy Question (I Think)

Easy Question (I Think)

I have a program that outputs a players Batting Average.

The Output var (BatAvg) is of Type Real.

When it prints the value of BatAvg to a file (or Screeen),
the format is as follows:

0.332

I can't figure out how to make the program chop off the zero
in front.

I have tried several formatting combinations, :4:3, :3:3, etc...
None seem to work.

Is there a bulit-in function that will chop off that zero for me?

Thanks,
Ray Mers
rm...@mdts3.mdhc.mdc.com

 

Re:Easy Question (I Think)


Quote
Ray wrote:
> I have a program that outputs a players Batting Average.

> The Output var (BatAvg) is of Type Real.

> When it prints the value of BatAvg to a file (or Screeen),
> the format is as follows:

> 0.332

> I can't figure out how to make the program chop off the zero
> in front.

> I have tried several formatting combinations, :4:3, :3:3, etc...
> None seem to work.

> Is there a bulit-in function that will chop off that zero for me?

I doubt that there is. If appearance is all that you are concerned with
then try something like:

WriteLn('.',(BatAvg*1000):3:0);

Of course if the batting average drops below .100 then the leading zeros
will be replaced by spaces: perhaps not what you wanted. You could
convert the number to a string:

Str(BatAvg:1:3,BatAvgStr);

Now you could do whatever you want with it. BatAvgStr will contain a
string version of what you don't want ("0.332"), but strings are easy to
manipulate. All we need to do now is remove the unwanted first
character.

Move(BatAvgStr[2],BatAvgStr[1],4);
Dec(BatAvgStr[0]);

In case you don't understand what just happened, the Move procedure
copies the last four characters to where the first is. The result
(".3322") is one character too long so the Dec procedure reduces the
length by one (".332"). BatAvgStr now contains the string representation
you want.

AME

Re:Easy Question (I Think)


Quote
Ray <rm...@mdts3.mdhc.mdc.com> wrote:
>0.332
>I can't figure out how to make the program chop off the zero
>in front.
>I have tried several formatting combinations, :4:3, :3:3, etc...
>None seem to work.
>Is there a bulit-in function that will chop off that zero for me?

No....write your own function to do so, though...not hard.

str(batavg, batstr);
batstr := copy(batstr, 2, 4);

Not hard at all...
:)

Re:Easy Question (I Think)


Quote
Glenn Grotzinger wrote:

> Ray <rm...@mdts3.mdhc.mdc.com> wrote:

> >0.332

> >I can't figure out how to make the program chop off the zero
> >in front.

> >I have tried several formatting combinations, :4:3, :3:3, etc...
> >None seem to work.

> >Is there a bulit-in function that will chop off that zero for me?

> No....write your own function to do so, though...not hard.

> str(batavg, batstr);
> batstr := copy(batstr, 2, 4);

> Not hard at all...

You obviously didn't try this before posting it as the solution. This
will _never_ produce the required result.

BatAvg := 0.777777

Result...
  BatStr = 7.77

Remember that BatAvg is a Real and will manifest itself in exponential
notation unless you force it not to. Your first line should have been:

Str(BatAvg:1:3, BatStr);

Result...
  BatStr = .778

Notice here that the result was also rounded, which I believe is the
desired effect.

Incidentally, the solution I posted:

Str(BatAvg:1:3, BatAvgStr);
Move(BatAvgStr[2], BatAvgStr[1], 4);
Dec(BatAvgStr[0]);

runs almost twice as fast because the Copy involves reassigning a value
to BatStr while Move just moves the bytes around the existing variable.
Of course, the speed difference is probably of little concern in this
application.

AME

Re:Easy Question (I Think)


In article <327E2B7D.2...@mdts3.mdhc.mdc.com>,

Quote
Ray  <rm...@mdts3.mdhc.mdc.com> wrote:

:0.332
:I can't figure out how to make the program chop off the zero
:in front.

function CHOPFN (x : real; dd : byte) : string;
var s : string;
begin
  Str (x:0:dd, s);
  if x >= 0 then
    chopfn := Copy (s,2,255)
  else
    chopfn := '-' + Copy (s,3,255);
end;

begin
  writeln (CHOPFN(0.322,3));
end.

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi  <URL:http://uwasa.fi/~ts>  ; FIN-65101,  Finland

Re:Easy Question (I Think)


"Alan M. Evans" <A.Michael.Ev...@airmail.net> wrote:

Quote
>You obviously didn't try this before posting it as the solution. This
>will _never_ produce the required result.

I KNEW IT WASN'T THE SOLUTION.

That's why I posted it....

The person who posted didn't obviously TRY it...so....

Re:Easy Question (I Think)


Thanks for the help everyone!!

The variety of answers really helps.

Guess what we learned in class last night (Monday)...
How to work with and manipulate strings :)

So all your examples make perfect sense now.

Thanks again,
Ray

Re:Easy Question (I Think)


Quote
In article <327E2B7D.2...@mdts3.mdhc.mdc.com> Ray <rm...@mdts3.mdhc.mdc.com> writes:
>From: Ray <rm...@mdts3.mdhc.mdc.com>
>Subject: Easy Question (I Think)
>Date: 4 Nov 1996 10:43:02 -0700
>I have a program that outputs a players Batting Average.
>The Output var (BatAvg) is of Type Real.
>When it prints the value of BatAvg to a file (or Screeen),
>the format is as follows:
>0.332
>I can't figure out how to make the program chop off the zero
>in front.
>I have tried several formatting combinations, :4:3, :3:3, etc...
>None seem to work.
>Is there a bulit-in function that will chop off that zero for me?
>Thanks,
>Ray Mers
>rm...@mdts3.mdhc.mdc.com

The answer to your explicit question is "No", but an answer to the implicit
question is to format the output in a string, using the Str procedure and then
the Copy function to chop off the leading zero.

I hope I've helped,
                               John.

Other Threads