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

# 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
• 3
• 2
• 2
• +3
2 Solutions

Commented:
You can remove thousand seperators with strreplace and if you do not want to lose decimal points use strtofloat
0

Author 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

Commented:
by using format

var
i: Extended;
s: String;

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

output will be 9,132.1256

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

output will be  \$1,000.12

0

Commented:
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
end;
0

Commented:
oops, you wanted the extra ','
change:
result := floattostrf(e,fffixed,18,2);
to:
result := floattostrf(e,ffNumber,18,2);
0

Author 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

Commented:
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

Commented:
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

Commented:
//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

Commented: