Browse Source

Fixed GUI coma as a decimal separator bug

PascalCoin 5 years ago
parent
commit
228d6481c9
1 changed files with 20 additions and 7 deletions
  1. 20 7
      src/core/UAccounts.pas

+ 20 - 7
src/core/UAccounts.pas

@@ -1923,24 +1923,37 @@ end;
 class function TAccountComp.TxtToMoney(const moneytxt: String;
   var money: Int64): Boolean;
 Var s : String;
+  LPosThousand, LPosDecimal : Integer;
+  LMoneyString : String;
 begin
   money := 0;
-  if Trim(moneytxt)='' then begin
+  LMoneyString := Trim(moneytxt);
+  if LMoneyString.Length=0 then begin
     Result := true;
     exit;
   end;
   try
-    If pos({$IFDEF DELPHIXE}FormatSettings.{$ENDIF}DecimalSeparator,moneytxt)<=0 then begin
-      // No decimal separator, consider ThousandSeparator as a decimal separator
-      s := StringReplace(moneytxt,{$IFDEF DELPHIXE}FormatSettings.{$ENDIF}ThousandSeparator,{$IFDEF DELPHIXE}FormatSettings.{$ENDIF}DecimalSeparator,[rfReplaceAll]);
-    end else begin
-      s := StringReplace(moneytxt,{$IFDEF DELPHIXE}FormatSettings.{$ENDIF}ThousandSeparator,'',[rfReplaceAll]);
+    LPosThousand := LMoneyString.IndexOf( TPCJSONData.JSONFormatSettings.ThousandSeparator );
+    LPosDecimal  := LMoneyString.IndexOf( TPCJSONData.JSONFormatSettings.DecimalSeparator );
+
+    if (LPosThousand>0) then begin
+      if (LPosThousand < LPosDecimal ) then begin
+        // Remove thousand values
+        LMoneyString := LMoneyString.Replace(String(TPCJSONData.JSONFormatSettings.ThousandSeparator),'',[rfReplaceAll]);
+      end else begin
+        // Possible 15.123.456,7890 format ( coma (,) = decimal separator )
+        // Remove decimal "." and convert thousand to decimal
+        LMoneyString := LMoneyString.Replace(String(TPCJSONData.JSONFormatSettings.DecimalSeparator),'',[rfReplaceAll]);
+        LMoneyString := LMoneyString.Replace(TPCJSONData.JSONFormatSettings.ThousandSeparator,TPCJSONData.JSONFormatSettings.DecimalSeparator,[rfReplaceAll]);
+      end;
     end;
-    money := Round( StrToFloat(s)*10000 );
+
+    money := Round( StrToFloat(LMoneyString,TPCJSONData.JSONFormatSettings)*10000 );
     Result := true;
   Except
     result := false;
   end;
+
 end;
 
 class procedure TAccountComp.ValidsEC_OpenSSL_NID(list: TList<Word>);