Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Percentage Calc 1

Status
Not open for further replies.

jfreak53

IS-IT--Management
Apr 30, 2004
44
GT
Ok here is what I have. I have two edit fiels. One where the person enters the dolar amount and the second where they enter the percent they want to add to this amount. I have tried everywhich a way I can think of to calculate percentages in delphi 6, no go ahhhhh. ha ha

So my question is how do I calculate the percentage of a number then add it to that number? Every time I try using a .39 for example I get an error saying it's not an integer or something of the sorts.

Thanks in advance.
 
Hey

Check this code, it works for me in delphi 7 :)

DFM file

object Form1: TForm1
Left = 192
Top = 114
Width = 519
Height = 241
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Edit1: TEdit
Left = 64
Top = 40
Width = 105
Height = 21
TabOrder = 0
Text = 'Enter $'
end
object Edit2: TEdit
Left = 184
Top = 40
Width = 97
Height = 21
TabOrder = 1
Text = 'Enter %'
end
object Edit3: TEdit
Left = 120
Top = 80
Width = 97
Height = 21
TabOrder = 2
end
object Button1: TButton
Left = 304
Top = 40
Width = 97
Height = 25
Caption = 'Button1'
TabOrder = 3
OnClick = Button1Click
end
end


PAS code

unit dollarpercentage;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c : Integer;

begin

a := StrToInt(Edit1.Text);
b := StrToInt(Edit2.Text);

c := (a * b) div 100;
c := c + a;

Edit3.Text := IntToStr(c);

end;

end.


Hope it helps :)

Regards,
Esh

 
Every time I try using a .39 for example I get an error saying it's not an integer or something of the sorts

0.39 is not an integer, it is a floating point value. You either need to specify to the user (TCaption or the like) the percentage value as input is to be a whole number, i.e. 39 for 39%, which is equal to 0.39, or you will have to turn the percentage value into a floating point value.

Code:
  Result.Text := FloatToStr((StrToFloat(OriginalAmount.Text) + StrToFloat(OriginalAmount.Text) * StrToFloat(Percentage.Text)));

Steve.
 
Perfect eshsam, worked like a charm. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top