Autor Beitrag
Arbengie
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66



BeitragVerfasst: Mo 21.12.09 18:42 
Hi, bin relativ neu in der Delphi-Programmierung und habe folgendes Problem, vllt könnt ihr mir helfen. :)

Und zwar gibt es bei meinem Programm eine normale Division, wo das Ergebniss e1:=z.B.:20,217617614 //e1=Variable

Wie kann ich jetzt machen dass das Programm das Ergebniss "e1" auf 2 Nachkommastellen rundet?



Danke im vorraus!
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mo 21.12.09 18:51 
Hallo und :welcome: im Forum
Guck dir mal in der Hilfe die funktion round an.
Damit kannst du runden. Wenn du zwei Nachkommastellen haben willst, musst du vorher das Komma verschieben (*100) und nachher wieder zurüch (/100).
mfg Boldar
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Mo 21.12.09 19:08 
Oder RoundTo benutzen und im 2. Parameter die Anzahl der Nachkommastellen angeben ;)

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Flepe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mo 21.12.09 22:18 
also ich has mi fomrat float gelernt ich miene es ging so
z.B.
lbl_ausgabe.caption:=format float('0.00',e1);

kann so funktionieren kan aber auch sein das in der klammer mehr doer wenier hochkommas müssen
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Mo 21.12.09 23:22 
Hey,

user profile iconFlepe hat folgendes geschrieben Zum zitierten Posting springen:
lbl_ausgabe.caption:=format float('0.00',e1);
so machst du aber n String drauß. Wenn er aber nur den FloatWert haben will, dann is RoundTo die besste Lösung. Ud der 2. Parameter von RoundTo gibt nicht die nachkommastellen an, sondern den Exponenten zur Basis 10. Alos wenn du 2 Nachkommastellen haben willst, dann ist das 10^-2 also is der 2. Parameter -2: e1 := RoundTo(e1, -2); Round To is in der Math-Unit die must du vorher noch in den Deklarationsteil schreiben...

Mfg Bergmann.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^


Zuletzt bearbeitet von Bergmann89 am Di 22.12.09 12:32, insgesamt 1-mal bearbeitet
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Di 22.12.09 00:04 
Alternativ könnte man als Datentyp auch Currency nehmen, das wäre genauer, wenn es eh nur um 2 Kommastellen geht.
Arbengie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66



BeitragVerfasst: Di 22.12.09 16:18 
user profile iconBergmann89 hat folgendes geschrieben Zum zitierten Posting springen:
Hey,

user profile iconFlepe hat folgendes geschrieben Zum zitierten Posting springen:
lbl_ausgabe.caption:=format float('0.00',e1);
so machst du aber n String drauß. Wenn er aber nur den FloatWert haben will, dann is RoundTo die besste Lösung. Ud der 2. Parameter von RoundTo gibt nicht die nachkommastellen an, sondern den Exponenten zur Basis 10. Alos wenn du 2 Nachkommastellen haben willst, dann ist das 10^-2 also is der 2. Parameter -2: e1 := RoundTo(e1, -2); Round To is in der Math-Unit die must du vorher noch in den Deklarationsteil schreiben...

Mfg Bergmann.


Wie schreibe ich das denn im Deklarationsteil??

In der Help habe ich das gefunden, komme aber damit nicht weiter.



RoundTo function

Rounds a floating-point value to a specified digit or power of ten using “Banker’s rounding”.

Unit

Math

Category

Arithmetic routines

type TRoundToRange = -37..37;
function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;

Description

Call RoundTo to round AValue to a specified power of ten.

AValue is the value to round.

ADigit indicates the power of ten to which you want AValue rounded. It can be any value from –37 to 37 (inclusive).

RoundTo uses “Banker’s Rounding” to determine how to round values that are exactly midway between the two values that have the desired number of significant digits. This method rounds to an even number in the case that AValue is not nearer to either value.
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Di 22.12.09 20:02 
Hey,

einfach ganz oben bei uses das Math mit reinschreiben.
Und dann kannst du die Funktion so benutzen wie ich oben schon geschrieben hab: e1 := RountTo(e1, -2); // auf 2 Nachkommastellen runden

MfG Bergmann

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Arbengie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66



BeitragVerfasst: Di 22.12.09 22:35 
Ja dass habe ich auch schon gelesen und mehrere haben anscheinend das selbe problem wie ich:

einfach math dahinter schreiben geht nicht, dann sagt mir delphi dass math nicht vorhanden sei.

und im ordner bei borland ist math.pas auch nicht da.

Benutze Borland Delphi 6

:/
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Di 22.12.09 23:05 
Hey,

Borland liefert nich den Quelltext (*.pas) mit, sondern die BinäryDatein (hieß das so bin mir grad nich sicher). Guck einfach ma ob du ne Math.dcu findest. Ich weiß nicht ob ich dir die einfach schicken kann, wegen den Rechten un so. Kennt sich da jmd aus? Kann ich die Math einfach hier online stellen?

MfG Bergmann.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
thepaine91
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: Mi 23.12.09 08:43 
Arbengie ich habe auch d6 und da ist eine Unit Math vorhanden. :O
Arbengie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66



BeitragVerfasst: Mi 23.12.09 17:18 
Also ich habe im Ordner "C:\Programme\Borland\Delphi6\Lib" jetzt eine Math.dcu gefunden, aber wenn ich nun das mache:

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

erscheint dies:

[Error] Unit1.pas(7): Declaration expected but identifier 'Math' found
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mi 23.12.09 17:53 
Warum steht wohl zwischen den schon eingetragenen Units ein Komma (,)??
kalmi01
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 39



BeitragVerfasst: Mi 23.12.09 19:32 
Moin moin,

warum nicht mal selber runden ?

ausblenden Quelltext
1:
f := Round(Zahl*100)/100;					

oder alternativ
ausblenden Quelltext
1:
f := Trunc(Zahl*100 + 0.5)/100;					




OT: wie bekommt man einen gerundeten Wert eigentlich wieder gerade ?
Arbengie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66



BeitragVerfasst: Mi 23.12.09 21:33 
user profile iconBoldar hat folgendes geschrieben Zum zitierten Posting springen:
Warum steht wohl zwischen den schon eingetragenen Units ein Komma (,)??


oh je, das war ein dummer Fehler von mir :S.

Aber danke, damit hat sich das Problem gelöst.

VIELEN DANK EUCH ALLEN! :)
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Do 24.12.09 14:52 
Hey,

user profile iconkalmi01 hat folgendes geschrieben Zum zitierten Posting springen:
OT: wie bekommt man einen gerundeten Wert eigentlich wieder gerade ?
wie meinen? Kommawerte könn doch gar nich gerade oder ungerade sein?!

MfG Bergmann.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^