Solved

how to calculate

Posted on 2007-03-22
16
615 Views
Last Modified: 2008-01-09
I would like to do some calculations on my form.
I have dbtext,currencyedit's and dbedit's.
Can you give me example how to calculate using these?
example:
I have a dbtext that gets a value from database.
Now I want that value to divide from a value in editbox
so a currency edit gives me the right sum..
just learning...
0
Comment
Question by:Vrtnar
[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
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 18770593
var a, b: Integer;
      c: Double;

if TryStrToInt(dbtext.caption, a) and tryToInt(dbedit.text, b) then
  c := b / a;

ziolko.
0
 

Author Comment

by:Vrtnar
ID: 18770706
dont understand a thing...
I tried --
var a, b: Integer;
      c: Double;
begin
if StrToInt(dbedit1.text, a) and StrToInt(edit14.text, b) then
currencyedit37.value := b / a;

and it dont work...please fro a nice explanation as I just learn
0
 

Author Comment

by:Vrtnar
ID: 18770769
if TryStrToInt(dbedit1.Text,a) and TryStrToInt(edit14.text,b) then
currencyedit37.value:= a / b;
does not work either...
0
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!

 
LVL 21

Expert Comment

by:ziolko
ID: 18770798
TDBEdit has property called Text
TDBEdit has property called Caption
both properties are of type string which means they can contain literlas
so if you want to do any mth calculation you need to convert them

there are 3 most commonly used functions:
1. StrToInt() this func will throw exception if it's not possible to convert
sample

procedure S1;
var s: string;
      i: Integer;
begin
  s := 'hello world';
  try
    StrToInt(s, i);
  except
    ShowMessage('cannot convert hello world to integer value');
  end;
end;

2. TryStrToInt() <- no error will be thrown

procedure S2;
var s: string;
      i: Integer;
begin
  s := 'hello world';
  if not TryStrToInt(s, i) then
    ShowMessage('cannot convert hello world to integer value');
end;

3. Val() <- no error will be thrown

procedure S3;
var s: string;
      i: Integer;
      val: Integer;
begin
  s := 'hello world';
  Val(s, i, code);
  if code <> 0 then
    ShowMessage('cannot convert hello world to integer value');
end;



ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18770821
what kind of values you have in edits?

ziolko.
0
 

Author Comment

by:Vrtnar
ID: 18770836
dbedit has no caption value but text.
in your 1st example to what do I assign   'c' ??
My 'c' should be a currency edit...
0
 

Author Comment

by:Vrtnar
ID: 18770837
in edits I have numbers only...
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18770918
my mistake TBText has Caption...

this should work unless you have floating point values in edit or text
if TryStrToInt(dbedit1.Text,a) and TryStrToInt(edit14.text,b) then
currencyedit37.value:= a / b;

if you have floating point use TryStrToFloat()

ziolko.
0
 

Author Comment

by:Vrtnar
ID: 18771054
dbedit1 is currency value in the database.
in edit14 i type in (freehand) number values.
I tried (on exit of the edit14):
procedure TPostavke.Edit14Exit(Sender: TObject);
var a, b: Integer;
      c: Double;
begin
if TryStrToInt(dbedit1.Text,a) and TryStrToInt(edit14.text,b) then
currencyedit37.value:= a / b;
end;
It does not work..
If i substitute TryStrToInt with TryStrToFloat i get :
[Error] Unit3.pas(255): There is no overloaded version of 'TryStrToFloat' that can be called with these arguments

You see I have a value of '584,65 €' in my dbedit1.
Now I want to type (example number 2 in my editbox) and when I leave the edit my currency displays
292,325...
0
 
LVL 21

Accepted Solution

by:
ziolko earned 125 total points
ID: 18771104
ok,

procedure TPostavke.Edit14Exit(Sender: TObject);
var a: Currency;
      b: Double;
begin
  if TryStrToCurr(dbedit1.Text,a) and TryStrToInt(edit14.text,b) then
    currencyedit37.value:= a / b;
end;

ziolko.
0
 

Author Comment

by:Vrtnar
ID: 18771208
procedure TPostavke.Edit14Change(Sender: TObject);
var a: Currency;
     b: double;
begin
if TryStrToCurr(dbedit1.Text,a) and TryStrToInt(edit14.text,b) then
    currencyedit37.value:= a / b;
end

I get :
[Error] Unit3.pas(256): Types of actual and formal var parameters must be identical
0
 
LVL 10

Expert Comment

by:dinilud
ID: 18771616
instead of TryStrToInt use TryStrToFloat
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18771651
what the ...??

i've already posted:
if TryStrToCurr(dbedit1.Text,a) and TryStrToFloat(edit14.text,b) then
    currencyedit37.value:= a / b;

where did it go??

ziolko.
0
 

Author Comment

by:Vrtnar
ID: 18773093
Dont seem to work...both ways...
0
 

Author Comment

by:Vrtnar
ID: 18773543
I think i have figured what is the mistake here..
a queery that is the info provider for dbedit1 has the field
marked as 'currency' (it is currency in the database however)
But when I remove the field type as 'currency' in the
field rooster and set it to currency false the code :
var a: Currency;
      b: integer;
begin
if TryStrToCurr(dbedit1.Text,a) and TryStrToInt(edit12.text,b) then
    currencyedit37.value:= a / b;
works !
is this ok ??
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18778162
if it works it's ok:)

ziolko.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 97
tvirtualstringtree freeze when load too manny images 10 94
Breakpoint doesn't stop in my variable 3 48
TAction.OnAfterExecute? 2 32
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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