943,721 Members | Top Members by Rank

Ad:
Jul 4th, 2009
0

Please help me with calculation in delphi.

Expand Post »
Hi i have another question this time about mathmatical calculations in delphi.

i have 3 labels on the form. Label1 is formatted like this 200/50, i need to use both parts of this caption in the calculation, the calculation needs to be 200 * .50 the 2nd Number in the label is a percentage, so i figure i should just be able to use .50, but in my program i dont want to have to see a . in the middle of the label

Label 2 is formatted like this 00" i would like to know how to ignore the inch character when using floating points.

Label 3 is the value being calculated, which is causing me yet another problem, because the calculation i need to perform is the 2 values in label one multiplied together X 2 then + 25.4 * Label 2 Value, but the total of label 2 needs to be multiplied within the equasion. Like 200 * .50 * 2 + (25.4 * Label2.Caption)

The final result which label 3 displays needs to be to 2 decimal places, i have been using StrToFloat And FloatToString to try this as i think floating points are the best way, but please correct me if im wrong.

thx for the help

PS. Label1 is formatted like XXX/XX i decided against using 2 or 3 labels to do it, because in the 2nd part of the program i need the same Option in a DropDown box, and it will still have to be formatted the same way XXX/XX in the list.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
revski is offline Offline
28 posts
since Jul 2009
Jul 4th, 2009
0

Re: Please help me with calculation in delphi.

is the .50 is 0.5 ?
Precedence rule..?
1. ()
2. not
3.*,/,div,mod,and
4.+,-,or,xor
5.<,>,=,<=,>=,<>,=
If there are same of precedence of operators,then the evaluating is left to right...
so
(200*0.5)*2+(25.4*Label2.Caption)
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Jul 4th, 2009
0

Re: Please help me with calculation in delphi.

Thanks for the reply,

just managed to cure my calculation problem about 5 mins before you posted, but because label 1 caption is in the format of XXX/XX which is 200/50 i had to use 3 seperate labels to do the job.

and for the 50 i needed to put a . before it, is there anyway i can use 1 Label in the format above and not have to put a . infront of the 2nd part to calculate it.

Because using 3 labels and .50 instead of 50 isnt correct in the software.

one more thing, i need to limit the caption of the label to 2 decimal places.
Last edited by revski; Jul 4th, 2009 at 8:27 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
revski is offline Offline
28 posts
since Jul 2009
Jul 4th, 2009
0

Re: Please help me with calculation in delphi.

Click to Expand / Collapse  Quote originally posted by revski ...

Label 2 is formatted like this 00" i would like to know how to ignore the inch character when using floating points..
I put 2 labels on the form and a button
Label1.caption is 3.12345
and label2.caption anything,cause the button will evaluate the result of label2.caption....
delphi Syntax (Toggle Plain Text)
  1.  
  2. Procedure TForm1.Button1Click(Sender: TObject);
  3.  
  4. Var
  5. after_dot_string:String;
  6. after_dot_array:Array[1..2]Of Char;
  7. i:Integer;
  8. Begin
  9.  
  10. {label1.caption is 3.12345,just example}
  11. after_dot_string:=Label1.Caption;
  12.  
  13. i:=0;
  14.  
  15. while (after_dot_string[i]<>'.') Do Begin
  16. Inc(i);
  17. If (after_dot_string[i]='.') Then Begin
  18. after_dot_array[1]:=after_dot_string[i+1];
  19. after_dot_array[2]:=after_dot_string[i+2];
  20. Break;
  21. End;
  22. End;
  23. Label2.Caption:='';
  24. For i:=1 To 2 Do Begin
  25. Label2.Caption:=Label2.Caption + after_dot_array[i];
  26. End;
  27. {label2.caption is 12}
  28. End;
Try it,it is works fine.....
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Jul 4th, 2009
0

Re: Please help me with calculation in delphi.

ok,the array has 3 element of char
delphi Syntax (Toggle Plain Text)
  1. Procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3. Var
  4. after_dot_string:String;
  5. after_dot_array:Array[1..3]Of Char; {1..3}
  6. i:Integer;
  7. Begin
  8.  
  9. {label1.caption is 3.12345,just example}
  10. after_dot_string:=Label1.Caption;
  11.  
  12. i:=0;
  13.  
  14. while (after_dot_string[i]<>'.') Do Begin
  15. Inc(i);
  16. If (after_dot_string[i]='.') Then Begin
  17. after_dot_array[1]:=after_dot_string[i]; {look at this}
  18. after_dot_array[2]:=after_dot_string[i+1];
  19. after_dot_array[3]:=after_dot_string[i+2];
  20. Break;
  21. End;
  22. End;
  23. Label2.Caption:='';
  24. For i:=1 To 3 Do Begin {and here 3 steps too}
  25. Label2.Caption:=Label2.Caption + after_dot_array[i];
  26. End;
  27. {label2.caption is .12}
  28. End;

Now you can win...
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Jul 4th, 2009
0

Re: Please help me with calculation in delphi.

Thanks FlamingClaw

solved the stupid problem i was having.
idea worked perfect

Thanks a bunch.
Reputation Points: 10
Solved Threads: 0
Light Poster
revski is offline Offline
28 posts
since Jul 2009
Jul 4th, 2009
0

Re: Please help me with calculation in delphi.

right,then mark as solved,thx
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Probably a silly question.. but im new to delphi :)
Next Thread in Pascal and Delphi Forum Timeline: generate sine waves with delphi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC