• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

converting string values from labels


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
dealclickcouk
Asked:
dealclickcouk
  • 3
  • 2
  • 2
  • +3
2 Solutions
 
alikoankCommented:
You can remove thousand seperators with strreplace and if you do not want to lose decimal points use strtofloat
0
 
dealclickcoukAuthor Commented:

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
 
LMuadDIbCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
MolandoCommented:
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
 
MolandoCommented:
oops, you wanted the extra ','
change:
result := floattostrf(e,fffixed,18,2);
to:
result := floattostrf(e,ffNumber,18,2);
0
 
dealclickcoukAuthor Commented:

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
 
TvigepCommented:
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
 
DavidBirch2dotComCommented:
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
 
MolandoCommented:
//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
 
DavidBirch2dotComCommented:
points split for valid answers?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now