Browse Source

Add Delphi-compatible TCurrencyHelper

Bi0T1N 2 years ago
parent
commit
eb62a71a5f
2 changed files with 113 additions and 0 deletions
  1. 90 0
      rtl/objpas/sysutils/syshelp.inc
  2. 23 0
      rtl/objpas/sysutils/syshelph.inc

+ 90 - 0
rtl/objpas/sysutils/syshelp.inc

@@ -1576,6 +1576,96 @@ begin
   Result:=TrimLeft(ATrimChars);
   Result:=TrimLeft(ATrimChars);
 end;
 end;
 
 
+{ ---------------------------------------------------------------------
+  TCurrencyHelper
+  ---------------------------------------------------------------------}
+function TCurrencyHelper.GetMaxValue: Currency;
+begin
+  Result:=MaxCurrency;
+end;
+
+function TCurrencyHelper.GetMinValue: Currency;
+begin
+  Result:=MinCurrency;
+end;
+
+function TCurrencyHelper.Ceil: Int64;
+begin
+  {$warning TCurrencyHelper.Ceil unimplemented!}
+end;
+
+function TCurrencyHelper.Floor: Int64;
+begin
+  {$warning TCurrencyHelper.Floor unimplemented!}
+end;
+
+function TCurrencyHelper.Frac: Currency;
+begin
+  Result:=System.Frac(Self);
+end;
+
+class function TCurrencyHelper.Parse(const S: string; const AFormatSettings: TFormatSettings): Currency;
+begin
+  Result:=StrToCurr(S, AFormatSettings);
+end;
+
+class function TCurrencyHelper.Parse(const S: string): Currency;
+begin
+  Result:=StrToCurr(S);
+end;
+
+class function TCurrencyHelper.Size: Integer;
+begin
+  Result:=SizeOf(Currency);
+end;
+
+function TCurrencyHelper.ToString(const AFormatSettings: TFormatSettings): string;
+begin
+  Result:=CurrToStr(Self, AFormatSettings);
+end;
+
+function TCurrencyHelper.ToString: string;
+begin
+  Result:=CurrToStr(Self);
+end;
+
+class function TCurrencyHelper.ToString(const Value: Currency; const AFormatSettings: TFormatSettings): string;
+begin
+  Result:=CurrToStr(Value, AFormatSettings);
+end;
+
+class function TCurrencyHelper.ToString(const Value: Currency): string;
+begin
+  Result:=CurrToStr(Value);
+end;
+
+function TCurrencyHelper.Trunc: Int64;
+begin
+  Result:=System.Trunc(Self);
+end;
+
+class function TCurrencyHelper.TryParse(const S: string; out Value: Currency; const AFormatSettings: TFormatSettings): Boolean;
+begin
+  Result:=True;
+  try
+    Value:=StrToCurr(S, AFormatSettings);
+  except
+    on E: Exception do
+      Result:=False;
+  end;
+end;
+
+class function TCurrencyHelper.TryParse(const S: string; out Value: Currency): Boolean;
+begin
+  Result:=True;
+  try
+    Value:=StrToCurr(S);
+  except
+    on E: Exception do
+      Result:=False;
+  end;
+end;
+
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
   TSingleHelper
   TSingleHelper
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}

+ 23 - 0
rtl/objpas/sysutils/syshelph.inc

@@ -203,6 +203,29 @@ Type
     property Length: SizeInt read GetLength;
     property Length: SizeInt read GetLength;
   end;
   end;
 
 
+  TCurrencyHelper = type helper for Currency
+  private
+    function GetMaxValue: Currency; inline;
+    function GetMinValue: Currency; inline;
+  public
+    function Ceil: Int64; inline;
+    function Floor: Int64; inline;
+    function Frac: Currency; inline;
+    class function Parse(const S: string; const AFormatSettings: TFormatSettings): Currency; overload; static;
+    class function Parse(const S: string): Currency; overload; inline; static;
+    class function Size: Integer; inline; static;
+    function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
+    function ToString: string; overload; inline;
+    class function ToString(const Value: Currency; const AFormatSettings: TFormatSettings): string; overload; inline; static;
+    class function ToString(const Value: Currency): string; overload; inline; static;
+    function Trunc: Int64; inline;
+    class function TryParse(const S: string; out Value: Currency; const AFormatSettings: TFormatSettings): Boolean; overload; inline; static;
+    class function TryParse(const S: string; out Value: Currency): Boolean; overload; inline; static;
+
+    property MaxValue: Currency read GetMaxValue;
+    property MinValue: Currency read GetMinValue;
+  end;
+
 {$IFDEF FPC_HAS_TYPE_SINGLE}
 {$IFDEF FPC_HAS_TYPE_SINGLE}
   TSingleHelper = Type Helper for Single
   TSingleHelper = Type Helper for Single
   Private
   Private