Autor Beitrag
Sors
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 47

Win XP
D7 Prof
BeitragVerfasst: Di 28.02.06 16:12 
Ich habe ein Programm geschrieben, dass mir für verschiedene Funktionen die Ergebnisse liefert. Das Ergebnis wird im Extended-Format ausgegeben. Nun soll das Ergebnis auf eine bestimmte Zahl Nachkommastellen gerundet werden, die der Anwender frei wählen kann.

Problem: Wenn ich die Funktion Round verwende, kann ich nicht die Nachkommastellen festlegen, auf die gerundet werden soll. Wenn ich die Funktion RoundTo (MathUnit) verwende, kann ich keine Extended-Zahlen übergeben, da RoundTo nur Double-Zahlen annimmt.

Kann mir bei meinem Problem jemand helfen?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 28.02.06 16:20 
Ergebnis mit 10^Nachkommastellen multiplizieren, Round anwenden und wieder durch 10^Nachkommastellen teilen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Hux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 171



BeitragVerfasst: Di 28.02.06 16:22 
Hiermit sollte es funken, achja, Math unit einbinden...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function RundeAufStelle(zahl: double; stellen: integer): double;
var multi: double;
begin
  multi:=IntPower(10, stellen);
  zahl:=round(zahl*multi);
  result:=zahl/multi;
end;


Hiyah...

War eigentlich des gleiche was BenBE gesagt hat..., nur in Delphi halt -.-


Zuletzt bearbeitet von Hux am Di 28.02.06 16:24, insgesamt 1-mal bearbeitet
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 28.02.06 16:22 
Oder, wenns nur um die Ausgabe geht: FloatToStrF. Andere Frage: Brauchst du wirklich Extended, oder reicht Double vielleicht auch aus?

_________________
We are, we were and will not be.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Di 28.02.06 16:36 
Round verwendet das kranke 'Bankers rounding', nimm lieber Int.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function RundeAufStelle(zahl: double; stellen: integer): double;
var multi: double;
begin
  multi:=IntPower(10, stellen);
  if zahl > 0 then
    zahl := Int (zahl * multi + 0.5)
  else
    zahl := Int (zahl * multi - 0.5);
  result:=zahl/multi;
end;

_________________
Na denn, dann. Bis dann, denn.
Sors Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 47

Win XP
D7 Prof
BeitragVerfasst: Mi 01.03.06 18:20 
Brauche Extended. Double reicht leider nicht aus.
Werd aber mal den Code oben versuchen, werd dann dementsprechend double in extended umbennenen.
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Mi 01.03.06 19:27 
Ansonsten könnte man ja über AND auch entsprechende Bits der Mantisse löschen das wäre dann zwar nur Trunc, aber über eine zweite Zahl könnte man das sogar ohne Abfrage in nen Round machen (gelobt seien die Normalisierten Zahlen).

EDIT: Mal wieder zu schnell geschossen. Das ist wohl nur für binäres Runden sinnvoll