Autor Beitrag
Adrian
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 314



BeitragVerfasst: Mi 10.12.03 11:59 
Servus!

Meine Berechnung erzeugt 3 maximal 2-stellige Gleitkomma-Zahlen, die dann so abgespeichert werden sollen:
12:34:56
also der Nachkommateil entfällt vollständig, er wird abgeschnitten, nicht gerundet.
Wenn die Zahl nur einstellig ist, soll eine führende Null hin, also so
01:02:03
Jetzt habe ich zwar verschiedene Formatierungen ausprobiert, aber das Beste, was ich hinbekommen habe war dann
1: 2: 3

Gibt es eine Formatierungsoption, die das automatisch macht, oder muß ich den Weg über eine Stringumwandlung gehen und die Null selber hinzufügen?

Gruß,

Adrian


Moderiert von user profile icontommie-lie: Topic verschoben


Zuletzt bearbeitet von Adrian am Mi 10.12.03 20:09, insgesamt 1-mal bearbeitet
Killi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 299

Win*
D6 Prof
BeitragVerfasst: Mi 10.12.03 12:05 
Eine Automatisierung gibt es wohl nicht, denn als Integer gibts keine Nullen und bei strings.......nee.....
am besten du machst das so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
if Length(stringzahl1) = 1 then
begin
   stringzahl1:= '0' + stringzahl1;
end;


das mit allen 3 Zahlen...fertig!

_________________
----
Life is hard and then you die
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Mi 10.12.03 13:17 
Format aus den SysUtils kann das nicht, aber Eugen Honeker hat eine modifizierte Format-Funktion geschrieben, die ohne die SysUtils auskommt (hauptsächlich für nonVCL-Programme) und die die API-Funktion wvsprintf benutzt, die das kann, was du willst.
U.A. ist diese Funktion in Luckie's nonVCL-Toolbox (http://www.luckie-online.de) zu finden, und so sieht sie aus:
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:
26:
27:
28:
29:
30:
{-----------------------------------------------------------------------------
  Procedure : Format
  Author    : Eugen Honeker
  Date      : 2003-02-08
  Purpose   : Formats a string according to the formatdiscriptors
  Arguments : fmt: string; params: array of const
  Result    : string
-----------------------------------------------------------------------------}

function Format(fmt: string; params: array of const): string;
var
  pdw1, pdw2: PDWORD;
  i: integer;
  pc: PCHAR;
begin
  pdw1 := nil;
  if length(params) > 0 then GetMem(pdw1, length(params) * sizeof(Pointer));
  pdw2 := pdw1;
  for i := 0 to high(params) do begin
    pdw2^ := DWORD(PDWORD(@params[i])^);
    inc(pdw2);
  end;
  GetMem(pc, 1024 - 1);
  try
    SetString(Result, pc, wvsprintf(pc, PCHAR(fmt), PCHAR(pdw1)));
  except
    Result := '';
  end;
  if (pdw1 <> nilthen FreeMem(pdw1);
  if (pc <> nilthen FreeMem(pc);
end;


Ein Beispielaufruf könnte so aussehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
label1.Caption := Format (
    '%02d:%02d:%02d'
    [byte(Trunc(2.3)), byte(Trunc(3.99)), byte(Trunc(32.5))]
  );


Vorsicht: Damit sich diese Format-Funktion nicht mit der aus SysUtils ins Gehege kommt, sollte man sie besser umbenennen (z.B. FormatExt) oder die SysUtils nicht mit einlinken, falls man sie nicht noch anderweitig braucht.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Mi 10.12.03 19:58 
Diverse Massen-Anschließungen entfernt.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert


Zuletzt bearbeitet von tommie-lie am Mi 10.12.03 21:04, insgesamt 1-mal bearbeitet
Adrian Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 314



BeitragVerfasst: Mi 10.12.03 20:09 
... und ich habe mich angeschlossen und bin sehr zufrieden. :D

Danke an Alle.

Gruß,

Adrian