![]() |
Delphi-Version: 10.4 Sydney
Format Extended mit Nullen
Hallo!
Ich komme grad mit dem Format von Fließkommawerten nicht so recht weiter:
Delphi-Quellcode:
Erwarten würde ich hier eine Ausgabe von "0.012.345,670" beim deutschen Locale. Heraus kommt aber "12345,670". Wie bekomme ich das hin dass die gewünschte Ausgabe rauskommt?
const
VALUE: Extended = 12345.67; begin ShowMessage(Format('%07.3f', [VALUE]), TFormatSettings.Create); end; Grüße Cody |
AW: Format Extended mit Nullen
Die Tausendertrenner bekommst du mit n statt f, aber die führenden Nullen setzt Format nur bei ganzen Zahlen ein (d).
Alternativ kannst du das gewünschte Ergebnis aber mit FormatFloat erreichen:
Delphi-Quellcode:
FormatFloat('0,000,000.000', VALUE, TFormatSettings.Create);
Ich würde auch empfehlen, das
Delphi-Quellcode:
nicht bei jedem Aufruf zu machen, da das jedes mal einen Haufen API-Aufrufe auslöst.
TFormatSettings.Create
|
AW: Format Extended mit Nullen
Zitat:
Zitat:
Zitat:
|
AW: Format Extended mit Nullen
So, das wäre jetzt meine Lösung. Evtl. ginge das ja noch effizienter, aber erstmal tuts was es soll:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
function FormatFloat2(const AValue: Extended; const ADigits, ADecimals: Byte; const AFormatSettings: TFormatSettings): string; var I, J: Byte; Fmt, Fmt2: string; begin Fmt := StringOfChar('0', ADigits); Fmt2 := ''; J := 1; for I := Fmt.Length downto 1 do begin Fmt2 := Fmt[I] + Fmt2; if J = 3 then begin Fmt2 := ',' + Fmt2; J := 1; Continue; end; Inc(J); end; Fmt2 := Fmt2 + '.' + StringOfChar('0', ADecimals); Result := FormatFloat(Fmt2, AValue, AFormatSettings); end; begin ShowMessage(FormatFloat2(12345.67, 7, 3, TFormatSettings.Create)); end; |
AW: Format Extended mit Nullen
Geht zumindest etwas schlanker.
Delphi-Quellcode:
Edit: Das muss natürlich L > 1 heißen.
function FormatFloat2(const AValue: Extended; const ADigits, ADecimals: Byte; const AFormatSettings: TFormatSettings): string;
begin var Fmt := string.Create('0', ADigits); var L := Fmt.Length - 3; while L > 1 do begin Insert(',', Fmt, L); Dec(L, 3); end; Fmt := Fmt + '.' + string.Create('0', ADecimals); Result := FormatFloat(Fmt, AValue, AFormatSettings); end; |
AW: Format Extended mit Nullen
Zitat:
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:12 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz