Solved

converting string values from labels

Posted on 2004-08-02
13
433 Views
Last Modified: 2010-04-05

Hi

I'm using Delphi 5, and have a form with several lables that contain values such as 1,134.34 etc....

I need to populate another label with the result of 2 labels, ie

label1.caption="1,134.34"
label2.caption="1,546.45"

label3.caption := (label1.caption+label2.caption);

I have tried the strtoint() func but it doesn't like the thousand separator, any suggestions on how to work around this would be great.

Many Thanks
0
Comment
Question by:dealclickcouk
  • 3
  • 2
  • 2
  • +3
13 Comments
 
LVL 4

Accepted Solution

by:
alikoank earned 25 total points
ID: 11694285
You can remove thousand seperators with strreplace and if you do not want to lose decimal points use strtofloat
0
 

Author Comment

by:dealclickcouk
ID: 11694791

thanks for the help....

just one other thing....

if I have the following...


label3.caption := (strtofloat(label1.caption)+strtofloat(label2.caption));

how do I now get label3.caption to show the result with thousand separators?

Thanks again, and sorry for the simple stuff, but still learning delphi...
0
 
LVL 4

Assisted Solution

by:LMuadDIb
LMuadDIb earned 25 total points
ID: 11695049
by using format

var
 i: Extended;
 s: String;

i := 9132.1256;
s := Format('%.2n', [i]);

output will be 9,132.1256

You can also add your own chars:

s := Format('$%.2n', [i]);

output will be  $1,000.12

0
 
LVL 2

Expert Comment

by:Molando
ID: 11695725
function AddNumStrings(s1,s2 : string) : string;
var
  t1,t2 : string;
  e : extended;
begin
  t1 := s1; t2 := s2;
  while  pos(',',t1) > 0 do delete(t1,pos(',',t1),1);
  while  pos(',',t2) > 0 do delete(t2,pos(',',t2),1);
  e := StrToFloat(t1) + StrToFloat(t2);
  result := floattostrf(e,fffixed,18,2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    showmessage(addnumStrings(label1.caption,label2.caption));
end;
0
 
LVL 2

Expert Comment

by:Molando
ID: 11695737
oops, you wanted the extra ','
change:
result := floattostrf(e,fffixed,18,2);
to:
result := floattostrf(e,ffNumber,18,2);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dealclickcouk
ID: 11702028

Oh, something I thought would be simple, seems so complicated!  Still all part of the learning curve!

I have tried a very basic example, just to get my head round it, Unfortunatly I can't use the AddNumStrings function example as in some cases I will be adding 5 labels together, in others 2 and again in others multiplying....

Anyway with the following code I get error ''5,555.55' is not a valid floating point value'.

var   test: Extended;
....
  test := strtofloat('5,555.55');

Any further help would be appreciated
0
 
LVL 2

Expert Comment

by:Tvigep
ID: 11702547
Because strtofloat does not accept Thousand separators:
"The DecimalSeparator global variable defines the character that must be used as a decimal point. Thousand separators and currency symbols are not allowed in the string. If S doesn't contain a valid value, StrToFloat raises an EConvertError exception. "

So you'd have to write a function that removes the Thousand separator:

function RemoveThouSep(s:string):string;
var i:integer;
     tmp:string;
begin
  tmp:='';
  for i:=1 to length(s) do
  begin
    if s[i]<>ThousandSeparator then
      tmp:=tmp+s[i];
  end;
  Result:=tmp;
end;

var   test: Extended;
....
  test := strtofloat(RemoveThouSep('5,555.55'));

You could even rewrite the function so that it immediately returns a float, if you wish.

HTH,
Stef
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11703446
An easy way to remove the thousand seporators would be to use the stringreplace function and replace all ',' 's with nothing

Commalesstext  := StringReplace(label1.caption, ',', '',
                          [rfReplaceAll, rfIgnoreCase]);

A great website for learning the basics of delphi, such as string and integer routines is www.delphibasics.co.uk 

for more help on this fucntion try

http://www.delphibasics.co.uk/RTL.asp?Name=StringReplace

David
0
 
LVL 2

Expert Comment

by:Molando
ID: 11713239
//this turns the string to a number
function NumIt(S : string) : extended;
var t1 : string;
begin
   t1 := s;
   while  pos(',',t1) > 0 do delete(t1,pos(',',t1),1);
   result :=  StrToFloat(t1);
end;

//turns the number to a string
function StringIt(e : extended) : string;
begin
  result := floattostrf(e,ffFixed,18,2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    showmessage(StringIt(NumIt(label1.caption) + NumIt(label2.caption)));
 //or you could do more
    showmessage(StringIt(NumIt(label1.caption) + NumIt(label2.caption) + NumIt(label3.caption) + NumIt(label4.caption)));
 
end;
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 13343528
points split for valid answers?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 139
Mobile Keyboard covers the display of the TMemo 3 78
find a node in VST 2 64
Downloading email attachments 2 68
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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

24 Experts available now in Live!

Get 1:1 Help Now