1. ObjectHelper

ObjectHelper提供了各种数据类型之间相互转换的函数,这里提供的函数比较简单,推荐使用

1.1. 相关属性和方法

  1. static function ToBoolean(o: TObject; Default: Boolean): Boolean; overload;

  2. static function ToBoolean(o: TObject): Boolean; overload;
    任意类型转换成Boolean类型

    • o: TObject 需要转换的数据
    • Default: Boolean 缺省值,如果转换错误,那么函数值取Default给定的值
  3. static function ToChar(o: TObject; Default: Char): Char; overload;

  4. static function ToChar(o: TObject): Char; overload;
    任意类型转换成Char类型

    • o: TObject 需要转换的数据
    • Default: Char 缺省值,如果转换错误,那么函数值取Default给定的值
  5. static function ToDelphiDateTime(o: TObject; Default: Double): Double; overload;

  6. static function ToDelphiDateTime(o: TObject): Double; overload;
    任意类型转换成TDateTime类型

    • o: TObject 需要转换的数据
    • Default: TDateTime 缺省值,如果转换错误,那么函数值取Default给定的值
  7. static function ToFloat(o: TObject): Double; overload;

  8. static function ToFloat(o: TObject; Default: Double): Double; overload;
    任意类型转换成Double类型

    • o: TObject 需要转换的数据
    • Default: Double 缺省值,如果转换错误,那么函数值取Default给定的值
  9. static function ToInt(o: TObject; Default: Longint): Longint; overload;

  10. static function ToInt(o: TObject): Longint; overload;

  11. static function ToInt64(o: TObject; Default: Int64): Int64; overload;

  12. static function ToInt64(o: TObject): Int64; overload;
    任意类型转换成整数类型

    • o: TObject 需要转换的数据
    • Default: Int64 缺省值,如果转换错误,那么函数值取Default给定的值
  13. static function ToString(o: TObject): string; overload;

  14. static function ToString(o: TObject; Default: string): string; overload;
    任意类型转换成字符串String类型

    • o: TObject 需要转换的数据
    • Default: String 缺省值,如果转换错误,那么函数值取Default给定的值

1.2. 用法示例

   1 var
   2   lParam: TBizParam;
   3   lTime: TDateTime;
   4 begin
   5   lParam := SystemUtils.TContextUtils.GetParam(Context, 'Biz:\SYSTEM\DATATIMEPARAMS.PARAMGROUP', 'ServerDate');
   6   jsDialogs.ShowMsg(ObjectHelper.ToString(lParam.Value), '当前日期1');
   7   lTime := ObjectHelper.ToDelphiDateTime(lParam.Value);
   8   jsDialogs.ShowMsg(ObjectHelper.ToString(lTime), '当前日期2');

2. 其他类型转换函数

2.1. 函数命名规则

  1. 源类型To目标类型
    把源类型转换成目标类型,例如:StrToInt就是从String类型转换成Int类型

  2. Try源类型To目标类型
    如果转换出错,则不提示错误,函数返回给出的缺省值,例如:
    StrToInt('11abc')会出错,而TryStrToInt('11abc', 4)函数的返回值是4

2.2. 目标类型为字符串

任何数据类型转换成字符串,都可以用Format函数

   1   static function StrToBool(S: string): Boolean;
   2   static function StrToBoolDef(S: string; Default: Boolean): Boolean;
   3   static function StrToCurr(S: string): Int64;
   4   static function StrToCurrDef(S: string; Default: Int64): Int64;
   5   static function StrToDate(S: string): Double;
   6   static function StrToDateDef(S: string; Default: Double): Double;
   7   static function StrToDateTime(S: string): Double;
   8   static function StrToDateTimeDef(S: string; Default: Double): Double;
   9   static function StrToFloat(S: string): Double;
  10   static function StrToFloatDef(S: string; Default: Double): Double;
  11   static function StrToInt(S: string): Longint;
  12   static function StrToInt64(S: string): Int64;
  13   static function StrToInt64Def(S: string; Default: Int64): Int64;
  14   static function StrToIntDef(S: string; Default: Longint): Longint;
  15   static function StrToTime(S: string): Double;
  16   static function StrToTimeDef(S: string; Default: Double): Double;
  17   static function TryEncodeDate(Year: Word; Month: Word; Day: Word; Date: Double): Boolean;
  18   static function TryEncodeTime(Hour: Word; Min: Word; Sec: Word; MSec: Word; Time: Double): Boolean;
  19   static function TryFloatToCurr(Value: Double; AResult: Int64): Boolean;
  20   static function TryFloatToDateTime(Value: Double; AResult: Double): Boolean;
  21   static function TryStrToBool(S: string; Value: Boolean): Boolean;
  22   static function TryStrToCurr(S: string; Value: Int64): Boolean;
  23   static function TryStrToDate(S: string; Value: Double): Boolean;
  24   static function TryStrToDateTime(S: string; Value: Double): Boolean;
  25   static function TryStrToFloat(S: string; Value: Real): Boolean; overload;
  26   static function TryStrToFloat(S: string; Value: Double): Boolean; overload;
  27   static function TryStrToInt(S: string; Value: Longint): Boolean;
  28   static function TryStrToInt64(S: string; Value: Int64): Boolean;
  29   static function TryStrToTime(S: string; Value: Double): Boolean;
  30   static function FloatToCurr(Value: Double): Int64;
  31   static function FloatToDateTime(Value: Double): Double;
  32   static procedure FloatToDecimal(Result: TFloatRec; Value: TObject; ValueType: TFloatValue; Precision: Longint; Decimals: Longint);
  33   static function FloatToStr(Value: Double): string;
  34   static function FloatToStrF(Value: Double; Format: TFloatFormat; Precision: Longint; Digits: Longint): string;
  35   static function FloatToText(BufferArg: string; Value: TObject; ValueType: TFloatValue; Format: TFloatFormat; Precision: Longint; Digits: Longint): Longint;
  36   static function FloatToTextFmt(Buf: string; Value: TObject; ValueType: TFloatValue; Format: string): Longint;
  37   static function IntToHex(Value: Longint; Digits: Longint): string; overload;
  38   static function IntToHex(Value: Int64; Digits: Longint): string; overload;
  39   static function IntToStr(Value: Longint): string; overload;
  40   static function IntToStr(Value: Int64): string; overload;
  41   static function DateTimeToFileDate(DateTime: Double): Longint;
  42   static function DateTimeToStr(DateTime: Double): string;
  43   static procedure DateTimeToString(Result: string; Format: string; DateTime: Double);
  44   static procedure DateTimeToSystemTime(DateTime: Double; SystemTime: Borland.Delphi.Windows._SYSTEMTIME);
  45   static function DateTimeToTimeStamp(DateTime: Double): TTimeStamp;
  46   static function DateToStr(DateTime: Double): string;
  47   static function CurrToStr(Value: Int64): string;
  48   static function CurrToStrF(Value: Int64; Format: TFloatFormat; Digits: Longint): string;
  49   static function BoolToStr(B: Boolean; UseBoolStrs: Boolean): string;
  50   static function Format(Format: string; Args: array of TObject): string;
  51   static function FormatBuf(Buffer: TObject; BufLen: Longword; Format: TObject; FmtLen: Longword; Args: array of TObject): Longword;
  52   static function FormatCurr(Format: string; Value: Int64): string;
  53   static function FormatDateTime(Format: string; DateTime: Double): string;
  54   static function FormatFloat(Format: string; Value: Double): string;


[[PageComment2()]]

数据类型转换 (last edited 2007-08-16 10:47:12 by alang)