Solved

converting string values from labels

Posted on 2004-08-02
13
432 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

21 Experts available now in Live!

Get 1:1 Help Now