Autor Beitrag
MrFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Do 05.05.05 12:36 
Hallo,

Ich habe momentan (mal wieder) ein kleines Problem mit großen Zahlen.

Wenn ich eine Extended-Variable, die eine Zahl mit mehr als 20 Stellen enthält, mittels FloatToStr in einen String umwandle,
wird automatisch das wissenschafliche Format verwendet (z.B. 1,34927987478384908E23).

Genau das will ich nicht! :evil:

Gibt's da vielleicht noch irgendeine andere Funktion, bei dieser Effekt verhindert werden kann :?:
iX0r
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 34

Ubuntu
D6, Lazarus
BeitragVerfasst: Do 05.05.05 13:06 
floattostrf heißt der befehl.
damit kannst du festlegen, wie dein extenden genau formatiert werden soll (also mit wievielen stellen).
mfg
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Do 05.05.05 13:18 
Des hab ich auch schon mal probiert.
Geht auch nicht. Egal was ich als Parameter übergebe, bei 18 Stellen oder höher wird wieder das wissenschaftliche Format ausgegeben.
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: Do 05.05.05 13:29 
Dann schreib Dir doch die Konvertier-Routine selber ...

Mit Trunc(Zahl/Power(10,Trunc(Log(Zahl)))) erhälst Du die erste Ziffer ... Wie das mit allen weiteren geht, sollte offensichtlich sein.

_________________
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.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Do 05.05.05 14:01 
Is mir aber leider nicht klar. :oops:
Die Funktion Log gibts nicht, hab Log10 genommen.
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: Do 05.05.05 14:22 
user profile iconBenBE hat folgendes geschrieben:
Mit Trunc(Zahl/Power(10,Trunc(Log(Zahl)))) erhälst Du die erste Ziffer ... Wie das mit allen weiteren geht, sollte offensichtlich sein.


Trunc(Zahl/Power(10,Trunc(Log10(Zahl)))) funktioniert relativ einfach:

Trunc(Log10(Zahl)) fragt ab, wieviele Zehner-Potenzen die Zahl umfasst.
Für 25 wäre dies Log10(25) = 1.39794 :arrow: Trunc(1.39794) = 1

Power(10, {...}) ermittelt dazu dann die großte 10er-Potenz, die kleiner oder gleich der gegebenen Zahl ist: Power(10, 1) = 10

Und im Letzten schritt wird mit Trunc(25 / {10}) = 2 die Ziffer ermittelt.

That's it ...

Um dann die nächste Ziffer zu erhalten, subtrahierst Du einfach 25-2*10 = 5 und wiederholst das, bis die Zahl 0 ist.

_________________
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.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Do 05.05.05 19:05 
Also, ich hab jetzt mal ne Funktion geschrieben
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure ZahlzuText(Zahl:extended);
var ermittelt: integer;
    Zahl1:extended;
    s:string;
begin
Zahl1 := power(10,Trunc(Log10(Zahl)));
s := '';

while Zahl > 0 do
  begin
  ermittelt := Trunc(Zahl/Power(10,Trunc(Log10(Zahl))));
  s := s + Inttostr(ermittelt);
  Zahl := Zahl - ermittelt*Zahl1;
  Zahl1 := Zahl1 / 10;
  end;
ShowMessage('Das Ergebnis: '+s);
end;

Die Funktioniert aber nur, wenn die Zahl nicht mehr als 21 Stellen und keine Nullen hat. :cry:

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt.
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: Do 05.05.05 19:39 
user profile iconMrFox hat folgendes geschrieben:
Also, ich hab jetzt mal ne Funktion geschrieben

[...]

Die Funktioniert aber nur, wenn die Zahl nicht mehr als 21 Stellen und keine Nullen hat. :cry:


ausblenden 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:
procedure ZahlzuText(Zahl:extended);
var
  ermittelt: integer;
  P:Integer;
  s:string;
begin
  P := Trunc(Log10(Zahl));
  s := '';
  If Zahl < 0 Then  //Beachten des Vorzeichens
  Begin
    s := '-';
    Zahl := -Zahl;
  end;

  while Zahl > 0 do
  begin  
    ermittelt := Trunc(Zahl/Power(10,P)));
    s := s + Chr(48+ermittelt); //Beschleunigtes Ermitteln der Ziffer, IntToStr braucht Ewigkeiten
    Zahl := Zahl - ermittelt*Power(10,P);
    Dec(P);  //Nächste Ziffer
    If (P=-1) and (Zahl>0) Then //Setzen des Kommas
      s := s + '.';
  end;
  ShowMessage('Das Ergebnis: '+s);
end;


Die müsste die Schwachstellen deiner Funktion ausbessern ;-)

_________________
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.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Do 05.05.05 20:08 
Wunderbar, endlich funzts. :D Vielen Dank.