Board index » delphi » splitting a floating point number

splitting a floating point number

Hello everyone,

I'm having a problem which is being annoyingly difficult to solve, I have
a double number, which I want to split up and end up with two strings
(actually two label captions) - One with the whole number part, and one
with the decimal point part. EG:

number: 123.456

I want to end up with:

label1.caption = 123

label2.caption = .456

The reason I want to split them up like this is so I can make label1 a
slightly larger font, and bold - as some of the numbers are quite large
and it can be confusing if you miss the decimal point for a thousanth
seperator.

I tried using:

label1.caption:=floattostrf(number, ffNumber,18,0);
label2.caption:=floattostrf(number - strtofloat(label1.caption),
ffNumber,18,precision);

which works except label2.caption has a leading 0 which i don't want..

Can anyone suggest a simple way to solve this?
(A fast/processor unintensive way would be preferable, but I'd be happy
just to get the damn thing working at the moment :)

Regards

Ogo
--
My Win9x Cursors: http://qjc.cjb.net/cursors.html
Please don't send me junk leaves! (take them out before replying).

No Silicon Heaven?  But where do all the calculators go? - Kryten.

 

Re:splitting a floating point number


Quote
OgO <quent...@leaves.qjc.cjb.net> wrote in message

news:Xns906CB2EA9BED2ogo1mynxwowaustcom@203.109.250.24...

Quote
> label1.caption:=floattostrf(number, ffNumber,18,0);
> label2.caption:=floattostrf(number - strtofloat(label1.caption),
> ffNumber,18,precision);

> which works except label2.caption has a leading 0 which i don't want..

Maybe if you strip it out!

label2.caption:=Delete(floattostrf(number - strtofloat(label1.caption),
ffNumber,18,precision), 1, 2);

But I think there must be a neater way...

--
Smola
--
"And once again, the day is saved"
(http://supersmola.tripod.com/index.htm)

Re:splitting a floating point number


Quote
"OgO" wrote:
> Hello everyone,

> I'm having a problem which is being annoyingly difficult to solve, I
have
> a double number, which I want to split up and end up with two
strings
> (actually two label captions) - One with the whole number part, and
one
> with the decimal point part. EG:

> number: 123.456

> I want to end up with:

> label1.caption = 123

> label2.caption = .456

<snip>

Hi,
You could convert the number first, and then just split the resluting
string into two strings.
Something like this:
{----------}
var
  s: string;
  i: Integer;
begin
  s := FloatToStr(number); { or another convert routine }
  { split string at '.' }
  i := Pos('.', s);
  Label1.Caption := Copy(S, 1, i - 1);
  Label2.Caption := Copy(S, i, MaxInt);
end;
{----------}

-ThomasN

Re:splitting a floating point number


Example:

var
  S: String;
  i: Integer;
begin
  S := FloatToStrF(number, ffNumber, 18, Precision);
  i := Pos(DecimalSeparator, S);
  if i > 0 then
  begin
    Label1.Caption := Copy(S, 1, i - 1);
    Label2.Caption := Copy(S, i, MaxInt);
  end
  else
  begin
    Label1.Caption := S;
    Label2.Caption := '';
  end;
end;

"OgO" <quent...@leaves.qjc.cjb.net> schreef in bericht
news:Xns906CB2EA9BED2ogo1mynxwowaustcom@203.109.250.24...

Quote
> Hello everyone,

> I'm having a problem which is being annoyingly difficult to solve, I have
> a double number, which I want to split up and end up with two strings
> (actually two label captions) - One with the whole number part, and one
> with the decimal point part. EG:

> number: 123.456

> I want to end up with:

> label1.caption = 123

> label2.caption = .456

> The reason I want to split them up like this is so I can make label1 a
> slightly larger font, and bold - as some of the numbers are quite large
> and it can be confusing if you miss the decimal point for a thousanth
> seperator.

> I tried using:

> label1.caption:=floattostrf(number, ffNumber,18,0);
> label2.caption:=floattostrf(number - strtofloat(label1.caption),
> ffNumber,18,precision);

> which works except label2.caption has a leading 0 which i don't want..

> Can anyone suggest a simple way to solve this?
> (A fast/processor unintensive way would be preferable, but I'd be happy
> just to get the damn thing working at the moment :)

> Regards

> Ogo
> --
> My Win9x Cursors: http://qjc.cjb.net/cursors.html
> Please don't send me junk leaves! (take them out before replying).

> No Silicon Heaven?  But where do all the calculators go? - Kryten.

Re:splitting a floating point number


Why don't you use string-operations???
Just do this:

   label1.caption:=floattostrf(number, ffNumber,18,0);
   s:=floattostrf(number -
strtofloat(label1.caption),ffNumber,18,precision);
   label2.caption:=copy(s,2,Length(s)-1);

where "s" is a string.

OgO <quent...@leaves.qjc.cjb.net> schrieb in im Newsbeitrag:
Xns906CB2EA9BED2ogo1mynxwowaust...@203.109.250.24...

Quote
> Hello everyone,

> I'm having a problem which is being annoyingly difficult to solve, I have
> a double number, which I want to split up and end up with two strings
> (actually two label captions) - One with the whole number part, and one
> with the decimal point part. EG:

> number: 123.456

> I want to end up with:

> label1.caption = 123

> label2.caption = .456

> The reason I want to split them up like this is so I can make label1 a
> slightly larger font, and bold - as some of the numbers are quite large
> and it can be confusing if you miss the decimal point for a thousanth
> seperator.

> I tried using:

> label1.caption:=floattostrf(number, ffNumber,18,0);
> label2.caption:=floattostrf(number - strtofloat(label1.caption),
> ffNumber,18,precision);

> which works except label2.caption has a leading 0 which i don't want..

> Can anyone suggest a simple way to solve this?
> (A fast/processor unintensive way would be preferable, but I'd be happy
> just to get the damn thing working at the moment :)

> Regards

> Ogo
> --
> My Win9x Cursors: http://qjc.cjb.net/cursors.html
> Please don't send me junk leaves! (take them out before replying).

> No Silicon Heaven?  But where do all the calculators go? - Kryten.

Re:splitting a floating point number


Quote
"OgO" <quent...@leaves.qjc.cjb.net> wrote in message

news:Xns906CB2EA9BED2ogo1mynxwowaustcom@203.109.250.24...

Quote
> Hello everyone,

> I'm having a problem which is being annoyingly difficult to solve, I have
> a double number, which I want to split up and end up with two strings
> (actually two label captions) - One with the whole number part, and one
> with the decimal point part. EG:

Label1.Caption := FloatToStrF (trunc (number), ffNumber, 18, 0);
Label2.Caption := FoatToStrF (frac (number), ffNumber, 18, precision);

Re:splitting a floating point number


In comp.lang.pascal.delphi.misc, on 22 Mar 2001, Smola announced:

Quote

>OgO <quent...@leaves.qjc.cjb.net> wrote in message
>news:Xns906CB2EA9BED2ogo1mynxwowaustcom@203.109.250.24...

>> label1.caption:=floattostrf(number, ffNumber,18,0);
>> label2.caption:=floattostrf(number - strtofloat(label1.caption),
>> ffNumber,18,precision);

>> which works except label2.caption has a leading 0 which i don't want..

>Maybe if you strip it out!

>label2.caption:=Delete(floattostrf(number - strtofloat(label1.caption),
>ffNumber,18,precision), 1, 2);

I tried this way, and when I run the program I keep getting the error
message:

Constant object cannot be passed as var parameter.

on that line above... I couldn't figure it out - I thought maybe it was
the label caption being passed - but when I copied the label caption to a
string and passed that instead I get the same error - the cursor is at the
very end of the line, after the semi-colon...

Thank you.

OgO
--
My Win9x Cursors: http://qjc.cjb.net/cursors.html
Please don't send me junk leaves! (take them out before replying).

No Silicon Heaven?  But where do all the calculators go? - Kryten.

Re:splitting a floating point number


In comp.lang.pascal.delphi.misc, on 22 Mar 2001, M.H. Avegaart
announced:

Quote
>Example:

>var
>  S: String;
>  i: Integer;
>begin
>  S := FloatToStrF(number, ffNumber, 18, Precision);
>  i := Pos(DecimalSeparator, S);
>  if i > 0 then
>  begin
>    Label1.Caption := Copy(S, 1, i - 1);
>    Label2.Caption := Copy(S, i, MaxInt);
>  end
>  else
>  begin
>    Label1.Caption := S;
>    Label2.Caption := '';
>  end;
>end;

Thanks, this works, it's a little bigger than I'd was hoping for, but it
gets the job done :)

Regards

OgO
--
My Win9x Cursors: http://qjc.cjb.net/cursors.html
Please don't send me junk leaves! (take them out before replying).

No Silicon Heaven?  But where do all the calculators go? - Kryten.

Re:splitting a floating point number


Quote
"OgO" <quent...@leaves.qjc.cjb.net> wrote in message

> >> label1.caption:=floattostrf(number, ffNumber,18,0);
> >> label2.caption:=floattostrf(number - strtofloat(label1.caption),
> >> ffNumber,18,precision);

> >> which works except label2.caption has a leading 0 which i don't want..

> >Maybe if you strip it out!

> >label2.caption:=Delete(floattostrf(number - strtofloat(label1.caption),
> >ffNumber,18,precision), 1, 2);

> I tried this way, and when I run the program I keep getting the error
> message:

> Constant object cannot be passed as var parameter.

AFAIK Delete is a procedure, not a function. Try

label2.caption := Copy (FloatToStrF (frac (number), ffNumber, 18,
precision), 2 {3 if you don't want the decimal point}, 1024);

Re:splitting a floating point number


Quote
Bruce Roberts <b...@bounceitattcanada.xnet> wrote in message

news:qJBu6.12557$TW.50586@tor-nn1.netcom.ca...

Quote
> AFAIK Delete is a procedure, not a function.

Yes it is. Sorry, my mistake!

Smola

Re:splitting a floating point number


In comp.lang.pascal.delphi.misc, on 23 Mar 2001, Bruce Roberts
announced:

Quote
>AFAIK Delete is a procedure, not a function. Try

>label2.caption := Copy (FloatToStrF (frac (number), ffNumber, 18,
>precision), 2 {3 if you don't want the decimal point}, 1024);

Yep, now that works - thank you!

Regards

OgO
--
My Win9x Cursors: http://qjc.cjb.net/cursors.html
Please don't send me junk leaves! (take them out before replying).

No Silicon Heaven?  But where do all the calculators go? - Kryten.

Other Threads