Explorar o código

+ Fixed currency<->string conversion support

michael %!s(int64=21) %!d(string=hai) anos
pai
achega
70143302f0
Modificáronse 3 ficheiros con 47 adicións e 4 borrados
  1. 5 1
      rtl/objpas/sysconst.pp
  2. 32 2
      rtl/objpas/sysutils/sysstr.inc
  3. 10 1
      rtl/objpas/sysutils/sysstrh.inc

+ 5 - 1
rtl/objpas/sysconst.pp

@@ -55,6 +55,7 @@ resourcestring
   SInvalidArgIndex       = 'Invalid argument index in format "%s"';
   SInvalidBoolean        = '"%s" is not a valid boolean.';
   SInvalidCast           = 'Invalid type cast';
+  SinvalidCurrency       = 'Invalid currency: "%s"';
   SInvalidDateTime       = '%f is not a valid date/time value.';
   SInvalidDrive          = 'Invalid drive specified';
   SInvalidFileHandle     = 'Invalid file handle';
@@ -208,7 +209,10 @@ end;
 end.
 {
   $Log$
-  Revision 1.9  2004-02-08 11:44:05  michael
+  Revision 1.10  2004-06-12 13:23:17  michael
+  + Fixed currency<->string conversion support
+
+  Revision 1.9  2004/02/08 11:44:05  michael
   + OSERRor related constants
 
   Revision 1.8  2004/01/20 23:05:31  hajny

+ 32 - 2
rtl/objpas/sysutils/sysstr.inc

@@ -1128,6 +1128,12 @@ Var
 
 Begin
   S:=StrPas(Buffer);
+  P:=Pos(ThousandSeparator,S);
+  While (P<>0) do
+    begin
+    Delete(S,P,1);
+    P:=Pos(ThousandSeparator,S);
+    end;
   P:=Pos(DecimalSeparator,S);
   If (P<>0) Then
     S[P] := '.';
@@ -1316,17 +1322,38 @@ begin
   Result:=Value;
 end;
 
-Function FloatToCurr (Const Value : Extended) : Currency;
+function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
+
 begin
+  Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
+  if Result then
+    AResult := Value;
+end;
+        
+function FloatToCurr(const Value: Extended): Currency;
 
+begin
+  if not TryFloatToCurr(Value, Result) then
+    Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
 end;
+              
 
 Function CurrToStr(Value: Currency): string;
+
 begin
+  Result:=FloatToStrF(Value,ffNumber,15,2);
 end;
 
 function StrToCurr(const S: string): Currency;
 begin
+  if not TextToFloat(PChar(S), Result, fvCurrency) then
+    Raise EConvertError.createfmt(SInValidFLoat,[S]);
+end;
+
+function StrToCurrDef(const S: string; Default : Currency): Currency;
+begin
+  if not TextToFloat(PChar(S), Result, fvCurrency) then
+    Result:=Default;
 end;
 
 function StrToBool(const S: string): Boolean;
@@ -2144,7 +2171,10 @@ const
 
 {
   $Log$
-  Revision 1.10  2004-04-28 20:48:20  peter
+  Revision 1.11  2004-06-12 13:23:17  michael
+  + Fixed currency<->string conversion support
+
+  Revision 1.10  2004/04/28 20:48:20  peter
     * ordinal-pointer conversions fixed
 
   Revision 1.9  2004/02/26 08:46:21  michael

+ 10 - 1
rtl/objpas/sysutils/sysstrh.inc

@@ -44,6 +44,10 @@ const
   MinDateTime: TDateTime = -657434.0;      { 01/01/0100 12:00:00.000 AM }
   MaxDateTime: TDateTime =  2958465.99999; { 12/31/9999 11:59:59.999 PM }
 
+  MinCurrency: Currency = -922337203685477.5807;  
+  MaxCurrency: Currency =  922337203685477.5807; 
+    
+
 Type
   TTextLineBreakStyle = (tlbsLF, tlbsCRLF);   // Must move to system unit, and add Mac tlbsCR too ?
 
@@ -131,8 +135,10 @@ Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue): Boolean;
 Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
 Function FloatToDateTime (Const Value : Extended) : TDateTime;
 Function FloattoCurr (Const Value : Extended) : Currency;
+function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
 Function CurrToStr(Value: Currency): string;
 function StrToCurr(const S: string): Currency;
+function StrToCurrDef(const S: string; Default : Currency): Currency;
 function StrToBool(const S: string): Boolean;
 function BoolToStr(B: Boolean): string;
 function LastDelimiter(const Delimiters, S: string): Integer;
@@ -178,7 +184,10 @@ function BCDToInt(Value: integer): integer;
 
 {
   $Log$
-  Revision 1.4  2004-02-26 08:46:21  michael
+  Revision 1.5  2004-06-12 13:23:17  michael
+  + Fixed currency<->string conversion support
+
+  Revision 1.4  2004/02/26 08:46:21  michael
   + Added AnsiSameStr
 
   Revision 1.3  2003/11/26 22:17:42  michael