Autor Beitrag
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 25.07.06 12:59 
Wie krieg ich FloatToStrF dazu, sich wie folgend zu verhalten: Es sollen maximal vier geltende Ziffern angezeigt werden, keine Darstellung mit Exponent (also ffFixed) und unnötige Nullen hinten nicht angehängt werden. Beispiele:

Single => String

0.001234 => 0.001 (round)
0.0001234 => 0
1.0e-5 => 0
99.994 => 99.99
99.996 => 100

etc.
n-regen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 202
Erhaltene Danke: 2



BeitragVerfasst: Di 25.07.06 13:36 
"Delphi für Kids" von Hans-Georg Schumann:
Zitat:
FloatToStrF(Zahl, Format, Genauigkeit, Kommastellen)
[...]
FloatToStrF(Input, ffNumber, 8, 2)
Hilft dir das weiter?
Übrigens:
Genauigkeit bringt nicht wirklich was.
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 25.07.06 13:45 
Nein, das hilft kein bisschen weiter.
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Di 25.07.06 14:35 
Der gute alte Format('%f',[zahl]) Befehl hilft da bestimmt. Genaueres hab ich aber bei der Hitze auhc nciht mehr im Kopf...
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 25.07.06 14:47 
Auch Format erlaubt mir nicht die gewünschte Ausgabe.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Di 25.07.06 15:47 
Hallo,

passt das ?
ausblenden Delphi-Quelltext
1:
Format('%.4g',[StrToFloat(Format('%.3f',[e]))]);					

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 25.07.06 15:51 
Nein, 10000 wird zu 1E004
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Di 25.07.06 15:55 
user profile iconSpaceguide hat folgendes geschrieben:
Nein, 10000 wird zu 1E004

und was soll bei 12345 herrauskommen?

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 25.07.06 15:57 
Es soll etwas wie diese Funktion, die ich mir gestrickt habe, machen, nur ein bisschen eleganter.

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:
type float = single;

function MyFloatToStr(const aFloat : float; aDigits : byte = 7): string;
{
 Converts the single precision variable aFloat to a string with
 aDigits-significant numbers

 Example : PointFloatToStr(1000.4,4) = '1000'
           PointFloatToStr(0.0001,4) = '0'
           PointFloatToStr(-123.46,4) = '-123.5'

}

 var dspos,left,right : integer;
     precision : integer;
begin
 precision := Max(Trunc(Ceil(ln((Abs(aFloat)+1))/ln(10))),aDigits);

 Result := FloatToStrF(aFloat,ffFixed,precision,10);
 dspos := Pos(DecimalSeparator,Result); //position of decimal separator
 if dspos>0 then
 begin
  left := Pos('-',Result)+1;

  if dspos-left<aDigits then
   right := left+aDigits
  else
   right := dspos;

  while Result[right]='0' do Dec(right); //remove trailing zeroes
  if Result[right]=DecimalSeparator then Dec(right); //remove unnecessary separator

  Result := Copy(Result,1,right);
 end;
end;
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 25.07.06 15:59 
user profile iconLannes hat folgendes geschrieben:
user profile iconSpaceguide hat folgendes geschrieben:
Nein, 10000 wird zu 1E004

und was soll bei 12345 herrauskommen?


Da soll dann natürlich 12345 rauskommen, genauso wie man es z.B. im Physikunterricht gemacht hat.
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Di 25.07.06 16:04 
Kannst doch versuchen, hinterher das E mit *10^ zu ersetzen und das dann aus zu rechnen? also du suchst dir die stelle mit dem E und machst dann vielleicht
ausblenden Delphi-Quelltext
1:
2:
3:
4:
ZahlHinterE := StrToInt(copy(result,pos('E', result), length(result)));
Result:=StringReplace(result, 'E'+IntToStr(ZahlHinterE), '', []);
for i:=1 to zahlHinterE do
   result:=result+0;

Kann aber sein, dass ich das problem nicht ganz verstanden hab ^^

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Di 25.07.06 23:23 
Hallo,

12345 bei 4 signifikanten Stellen = 12345 :gruebel:

kannst ja mal das antesteten:
:!: unit math einbinden

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.Button3Click(Sender: TObject);
var aDigits : byte;
    e : extended;
begin
  aDigits := StrToInt(Edit2.Text);
  e := StrTofloat(Edit1.Text);
  if Round(Log10(ABS(e)))+1 > aDigits then
    Label1.Caption := IntToStr(round(e))//round oder trunc  ????
    else
      Label1.Caption := Format('%.'+IntToStr(aDigits)+'g',
                               [StrToFloat(Format('%.'+IntToStr(aDigits-1)+'f',[e]))]
                               );

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Mi 26.07.06 08:08 
user profile iconLannes hat folgendes geschrieben:

12345 bei 4 signifikanten Stellen = 12345 :gruebel:


Nunja, wenn ich mich recht erinnere, war das im Physikunterricht so. Man sollte ja die Endergebnisse mit einer sinnvollen Stellenzahl angeben. Vor dem Komma wurde aber nix weggeschnippelt.
Sy-
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 177



BeitragVerfasst: Mi 26.07.06 09:55 
Genügt nicht einfach folgendes:
ausblenden Delphi-Quelltext
1:
2:
3:
Zahl:=123456.78634;
Fa:=100//das wären 2 Stellen hinterm Komma
result:=round(Zahl*Fa)/Fa;

da wird auch nichts abgekürzt oä.

Hier mal als Function wie du es brauchst:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
function myRound(Zahl:String; Stellen:integer=2):string;
var
  fa,x:integer;
  gZahl:real;
begin
  fa:=1;
  for x := 0 to fa do fa:=fa*10;
  
  try
    gZahl:=strtofloat(Zahl);
    result:=floattostr(round(gZahl*fa)/fa);
  except
    result:='0';
  end;
end;


Gruß

_________________
Problems connecting People
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Mi 26.07.06 10:27 
Testet doch einfach mal eure Funktionen gegen die oben genannten Anforderungen, bevor ihr sie postet.
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Mi 26.07.06 11:31 
o.k. aber wie sind denn die genauen Bedingungen?

Zitat:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function MyFloatToStr(const aFloat : float; aDigits : byte = 7): string;  
{  
 Converts the single precision variable aFloat to a string with  
 aDigits-significant numbers  
 
 Example : PointFloatToStr(1000.4,4) = '1000'  
           PointFloatToStr(0.0001,4) = '0'  
           PointFloatToStr(-123.46,4) = '-123.5'  
 
}

Reicht mir nicht so richtig, wann muss Vorzeichen beachtet werden usw...


0.001234 => 0.001 (round) Also alle Nachkommas auf 3 Stellen?
0.0001234 => 0 Sobald 4 Stellen keine Kommas=0?
1.0e-5 => 0 Verdammt kleine Zahlen auf =0
99.994 => 99.99 Bei positiven Zahlen auch 2 Nachkommastellen?
99.996 => 100 Bei allen Zahlen aufrunden?
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Mi 26.07.06 11:40 
die funktion ist getestet und in mehreren anwendungen im einsatz... ich habe das mit deinen "testzahlen" von oben auch getestet... und es funktioniert...

ABER schau mal selbst bei dir oben: du hast in deinen Musterlösungen einen unterschiedliche Anzahl von erlaubten Nachkommastellen

hier: 0.001234 => 0.001 (round)
hier: 99.994 => 99.99

dementsprechend musst du das natürlich im Offset-Parameter anpassen.
Desweiteren wird hier richtig gerundet und nicht im Delphi- typischen
Banker's Rounding (bei einem exakten Nachkomma- Anteil von .5 wird immer zur "gerade" Zahl gerundet).

Um die Funktion nutzen zu können, musst du die Unit Math einbinden

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function RoundEx(const X: Extended; const Offset: integer): Extended;
begin
  if frac(X * Power(10, OffSet)) >= 0.5 then
       result := ceil(X * Power(10, OffSet)) / Power(10, OffSet)
  else result := floor(X * Power(10, OffSet)) / Power(10, OffSet);
end;


um das dann wie von dir gewünscht einsetzen zu können, kannst du dann bespielsweise
ausblenden Delphi-Quelltext
1:
  Label1.Caption := FloatToStr(RoundEx(StrToFloat(edit1.Text), 3));					


aufrufen.

C.

Anmerkung noch am Rande: Patzig den Leuten gegenüber sein, die dir helfen wollen, ist nicht die feine Art.
Sy-
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 177



BeitragVerfasst: Mi 26.07.06 11:49 
Spaceguide, jetzt wird es zwar OT aber:

Arbeite mit den Scripten die du bekommst und verlang nicht auf dich exakt geschneiderte Scripte.
Wir sind hier um dir zu helfen, nicht um deine Arbeit zu erledigen.

Meins funktioniert nämlich auch und wie crowley schon sagte muss ein parameter mehr rein.

Fällt mir schwer zu glauben, dass du den anderen Script selber gecoded hast wenn du nichtmal unsere Scripte anwenden kannst.

Gruß

_________________
Problems connecting People
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Mi 26.07.06 16:07 
Jaja.. Sy- wird wieder ausfallend, offensiv und spricht von oben herab, nur um zu verdecken, dass er das Problem nicht verstanden hat.
Hier gehts darum, eine Zahl nicht auf ihre Nachkommastellen, sondern auf ihre stellen Insgesammt zu runden, oder? Also während 1,12344 zu 1,234(4?) wird, wird 12,344 zu 12,34(4?), oder? Versuch doch noch ein paar mehr Beispiele zu geben, und wenn du im endeffekt nur 4 ziffern haben willst, was wird dann aus 12345? die zahl verändert sich ja maßgeblich, wenn du einfach eine stelle weglässt, weil du keine 5 sondern 4 ziffern haben willst.

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 26.07.06 16:49 
Probiere mal das:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function GerundeteZahl(Zahl : Extended; Rundung : Integer) : Extended;
begin
  while trunc(zahl) > power(10, rundung) do inc(rundung);
  result := RoundTo(zahl, Rundung);
end;

Du benötigst die Unit Math.
Ich habs nur mit zwei Werten getestet. Als Rundungswert musst du in deinen Beispielen -3 vorgeben.
Für weitere Infos kannst du auch in die Delphi-Hilfe schauen.


Zuletzt bearbeitet von jasocul am Mi 26.07.06 16:53, insgesamt 1-mal bearbeitet