Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Fehler in Zint QR-EPC-Code (https://www.delphipraxis.net/207869-fehler-zint-qr-epc-code.html)

WoGe 10. Mai 2021 21:54

Fehler in Zint QR-EPC-Code
 
Die Formatierung des Amount Strings in der Zint OREPC-Lib ist bei mir bei Beträgen über 1000€ falsch.

Es ist möglich das ich etwas (noch) nicht gefunden habe um das Problem anders zu beheben.
Folgender Code löst das Problem zumindest auf meinem Rechner:
(Die Ausgeremten Zeilen sind der Original-Code)
Delphi-Quellcode:
function TZintEPCQR.GetAmountAsString: String;
var s : string;
    l : Integer;
begin
//  Result := Format('%n',[FAmount]);
//  Result := 'EUR'+ReplaceText(Result,',','.');
    s:= Format('%n',[FAmount]);
    s := s.Replace(',','');
    s := s.Replace('.','');
    l := s.Length;
    s := 'EUR' + s.Substring(0,l-2) + '.' + s.Substring(l-2,2);
    Result := s;
end;
Der so erzeugte QR-Code wird von der Banking-App als richtig und ausführbar erkannt.

Geht das eleganter oder auch ganz anders?

himitsu 10. Mai 2021 21:59

AW: Fehler in Zint QR-EPC-Code
 
%n fügt nunmal Tausendertrenner ein.


Format hat einen optionalen FormatSettings-Parameter.

Warum kommt immernoch jemand auf die blöde komische Idee mit Replace rumpfuschen zu müssen,
um das Probem der sprachabhängigen Dezimaltrennzeichen zu lösen?



Und rate mal was mit einen Substring passiert, wenn es keine 2 Nachkommastellen gibt. (wobei man die 2 im Format-String erzwingen könnte)

WoGe 10. Mai 2021 22:38

AW: Fehler in Zint QR-EPC-Code
 
Hi
Wo er Recht hat hat er Recht:
Delphi-Quellcode:
function TForm1.AmountStr: string;
var
  formatSettings : TFormatSettings;
begin
  formatSettings := TformatSettings.Create(LOCALE_SYSTEM_DEFAULT);
  formatSettings.ThousandSeparator := Char(0);
  formatSettings.DecimalSeparator := Char('.');
  formatSettings.CurrencyString := '';
  Result := 'EUR'+CurrToStrF(myCurrency, ffCurrency, 2, formatSettings);
end;
So gehts auch und ist eleganter
Ich baue das gleich mal in die Lib ein

Danke für den Hinweis

Edit:
Das hier ist die geprüfte Ersatzroutine für die Lib:
Delphi-Quellcode:
function TZintEPCQR.GetAmountAsString: String;
var
  formatSettings : TFormatSettings;
begin
  formatSettings := TformatSettings.Create(LOCALE_SYSTEM_DEFAULT);
  formatSettings.ThousandSeparator := Char(0);
  formatSettings.DecimalSeparator := Char('.');
  formatSettings.CurrencyString := '';
  Result := ('EUR'+CurrToStrF(FAmount, ffCurrency, 2, formatSettings)).TrimRight;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:19 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz