Autor Beitrag
V!P3R
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151



BeitragVerfasst: Di 24.03.09 20:30 
Hi,
Ich hab eine zahl, zum beispiel 3,14159265 (die steht jez in einem Edit) und möchte diese jetz auf drei nachkommastellen runden.
wie mache ich das?

MfG
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Di 24.03.09 20:33 
Guck dir FloatToStrF und/oder Format an.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
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: Di 24.03.09 20:44 
Das geht mit Round.

KLICK MICH

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

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Di 24.03.09 21:00 
Jo, das rundet die Zahl, aber es kann wegen der Ungenauigkeit beim Speichern von Fließkommazahlen dazu kommen, dass dann einige 0en ausgegeben werden und dann ab der 10. Nachkommastelle Zahlenmüll anfängt.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
GlasshouseManor
Hält's aus hier
Beiträge: 10


Delphi Enterprise
BeitragVerfasst: Do 26.03.09 23:32 
Zum Runden auf 3 Nachkommastellen nimmst Du Deine Zahl *1000, machst ein TRUNC und dividierst wieder durch 1000. Dann hast Du den Zahlenmüll wech.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Do 26.03.09 23:36 
Edit: Ähh, hab mich vertan, sorry.

_________________
PROGRAMMER: A device for converting coffee into software.
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: Do 26.03.09 23:37 
Dann ist aber nicht gerundet:
ausblenden Quelltext
1:
2:
3:
2,8888888~2,889
und nicht
2,8888888~2,888
P-A-L
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 30



BeitragVerfasst: Do 26.03.09 23:51 
user profile iconBoldar hat folgendes geschrieben Zum zitierten Posting springen:
Dann ist aber nicht gerundet:
ausblenden Quelltext
1:
2:
3:
2,8888888~2,889
und nicht
2,8888888~2,888


Dann macht man halt
ausblenden Delphi-Quelltext
1:
zahl := ( trunc(round(zahl*1000)) / 1000 );					
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Fr 27.03.09 00:04 
Oder man nimmt die fertige Funktion RoundTo, wie oben schon verlinkt, dann kann man sich diese umständliche Version sparen.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Fr 27.03.09 08:29 
Da nicht alle Gleitkommazahlen exakt abzubilden sind, ist es ergo auch unmöglich jede Zahl mit z.B. zwei Nachkommastellen in einem Double exakt abzubilden. Das ist so und lässt sich nicht ändern. Am Besten, man ignoriert diesen umstand beim rechnen und rundet nur entsprechend, um die Zahl darzustellen.

_________________
Na denn, dann. Bis dann, denn.