Solved

# converting string values from labels

Posted on 2004-08-02
436 Views

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
Question by:dealclickcouk
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +3

LVL 4

Accepted Solution

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

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

ID: 11695049
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

LVL 2

Expert Comment

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

LVL 2

Expert Comment

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

Author Comment

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

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

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

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

ID: 13343528
0

## Featured Post

Question has a verified solution.

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

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â€¦
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGridâ€¦
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templaâ€¦
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
###### Suggested Courses
Course of the Month3 days, 9 hours left to enroll