Autor Beitrag
origin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

WinXP Linux Irix
Delphi 7 Professional, Delphi 2007
BeitragVerfasst: Mo 23.08.10 08:56 
Hallo zusammen,
wie kann man eingetlich z.B. zwei Werte mit Komma berechnen?

Wie z.B. sowas:

0,03 * 0,9

Herausgefunden habe ich, dass ich Ganzzahlen per Integer berechnen kann.
Doch wie muss ich sowas einfaches bei Werten mit Komma schreiben? Stehe aufm Schlauch. :autsch:


Hier mal mein einfaches Listing:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
zahl1, zahl2, ergebnis: integer;
begin
    zahl1:=StrToInt(Edit1.Text);
    zahl2:=StrToInt(Edit2.Text);
    ergebnis:=zahl1*zahl2;
    Label1.Caption:=IntToStr(ergebnis);
end;

end.

Bin für Eure Hilfe sehr dankbar.

Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
Moderiert von user profile iconNarses: Topic aus Dateizugriff verschoben am Mo 23.08.2010 um 09:15
uko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 220
Erhaltene Danke: 1

Win XP, VISTA, WIndows 7
Delphi 2007/2010 Prof
BeitragVerfasst: Mo 23.08.10 09:06 
Schau Dir mal den Datentyp Double an bzw. StrToFloat

Grüße,
Uli
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mo 23.08.10 09:06 
Zunächst mal wären Delphi-Tags für den Source-Code nicht schlecht.
Zu deiner Frage:
Du musst auch einen passenden Typ für deine Vriablen verwenden. Integer geht nur für ganzzahlige Werte. Nimm statt Integer mal Double. Dann sollte es funktionieren.
Allerdings benötigst du Werte mit einem Dezimalpunkt, statt eines Komma. Also bei der Eingabe aufpassen. :wink:
origin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

WinXP Linux Irix
Delphi 7 Professional, Delphi 2007
BeitragVerfasst: Mo 23.08.10 09:30 
Hm, freue mich über Eure Hilfe,
doch wie muss ich das Schreiben mit dem "double"?

Hab da kein Plan.

StrToDouble oder wie?
uko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 220
Erhaltene Danke: 1

Win XP, VISTA, WIndows 7
Delphi 2007/2010 Prof
BeitragVerfasst: Mo 23.08.10 09:45 
Die Hilfe ist dein Freund! :wink: Im Prinzip geht es genau wie bei Integer nur halt mit Double und StrToFloat.
origin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

WinXP Linux Irix
Delphi 7 Professional, Delphi 2007
BeitragVerfasst: Mo 23.08.10 09:48 
Hm, grübel grübel. :gruebel:

---Moderiert von user profile iconNarses: Beiträge zusammengefasst---

Sorry aber ich komme da so nicht weiter.
Kann mir jemand listen wie sowas aussehen soll?
Danke
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Mo 23.08.10 10:05 
Besser StrToFloatDef() oder TryStToFloat()

ausblenden Delphi-Quelltext
1:
zahl1:=StrToFloatDef(Edit1.Text, 0); //Wenn nicht konvertiert werden kann, wird 0 verwendet.					

_________________
Markus Kinzler.
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Mo 23.08.10 11:08 
ausblenden Delphi-Quelltext
1:
2:
3:
var
 meineGanzZahl: Integer;
 meineKommaZahl: Double;

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
origin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

WinXP Linux Irix
Delphi 7 Professional, Delphi 2007
BeitragVerfasst: Mo 23.08.10 11:14 
Finds ja echt lieb aber ich raffs net.

Habs so versucht. Geht net:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
var
zahl1, zahl2, ergebnis: Float;
begin
    zahl1:=StrToFloatDef(Edit1.Text, 0);
    zahl2:=StrToFloatDef(Edit2.Text, 0);
    ergebnis:=zahl1+zahl2;
    Label1.Caption:=StrToFloatDef(ergebnis);
end;

Vielleicht kann mir jemand meinen oben geposteten Code so verändern, dass ich mal sehe, wie man so was schreiben muss, damit mein minirechner da funzt. Danke.

Moderiert von user profile iconNarses: Überflüssige Zeilenumbrüche/Leerzeilen entfernt.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Mo 23.08.10 11:16 
ausblenden Delphi-Quelltext
1:
Label1.Caption:=FloatToStr(ergebnis);					

_________________
Markus Kinzler.
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mo 23.08.10 11:17 
Ich denke, du solltest dich erst einmal mit den Grundlagen von Delphi vertraut machen. Da gibt es einige nette Bücher mit denen man das ratz fatz lernt.

Zum konkreten Problem:

zahl1, zahl2 und ergebnis sind vom Typ Float.
Die Ausgabe in Label1.Caption ist wie der Text der Edit Felder vom Typ String.

StrToFloat wandelt wie der Name erahnen lässt von String nach Float um.
Folglich musst du das Gleiche zur Ausgabe rückwärts machen, sprich: FloatToStr.

Hilft dir das weiter?

_________________
Delphi: XE - OS: Windows 7 Professional x64
origin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

WinXP Linux Irix
Delphi 7 Professional, Delphi 2007
BeitragVerfasst: Mo 23.08.10 11:27 
Ahhhhhhaaa ! :idea:

Jetzt hat geschnackelt und gefunzt.

Der Tipp war sehr hilfreich.
Vielen lieben Dank.

---Moderiert von user profile iconNarses: Beiträge zusammengefasst---

Doch eine Frage ist doch noch offen.
Wie kann ich das machen, damit die Ausgabe eine GANZZAHL ist?
Vermutlich mit StrToInt??? Nur wie genau?
Stundenplan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 128
Erhaltene Danke: 32

Win 7
Delphi 7 Pers., C# (VS 2010 Express)
BeitragVerfasst: Mo 23.08.10 13:30 
Round() oder Trunc() wäre eine Möglichkeit!
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Mo 23.08.10 13:39 
Moin!

Runden mit Round(kaufmännisch); Ceil(aufrunden); Trunc(abrunden), RoundTo(kaufmännisch, auf n Stellen).

Die ersten drei geben dir einen Wert des Typs Integer zurück, hier muss also wieder IntToStr genommen werden - nicht FloatToStr.

Für Ceil und RoundTo musst du die Unit Math einbinden(uses Math; zu Beginn des Interface- oder Implementation-Teils).

lg,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Dude566
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mo 23.08.10 15:49 

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Mo 23.08.10 17:31 
Ein Tutorial fehlt noch finde ich: Delphi-Wikibook

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.