破釜沉舟论坛 » 编程开发 » 自己写的整型转字符串型函数(Delphi)

2006-4-7 22:16 祥子
自己写的整型转字符串型函数(Delphi)

  这里用到了Math单元

function IntReplace(Source: Integer): Char;  //转换
begin
   case Source of
     0: Result := '0';
     1: Result := '1';
     2: Result := '2';
     3: Result := '3';
     4: Result := '4';
     5: Result := '5';
     6: Result := '6';
     7: Result := '7';
     8: Result := '8';
     9: Result := '9';
   end;
end;

function AIntToStr(Source: Integer): string; //返回要的结果
var
   AHigh, I: Integer;   //AHigh为输入整数的最高位数
   tmpChar: array of Char;  //动态数组,保存结果
   ResultStr: string;
begin
   ResultStr := '';
   AHigh := Trunc(Log10(Source)) + 1; //Trunc为取整,Log10为求以10为底的对数
   SetLength(tmpChar, AHigh);         //初始化数组的大小为整数的最高位
   for I := Low(tmpChar) to High(tmpChar) do //从数组的下标到上标循环
   begin
       tmpChar[I] := IntReplace(Source mod 10); //mod表示取模
       Source := Source div 10;  //div表示整除,去掉小数部分
   end;
   for I := High(tmpChar) downto Low(tmpChar) do //从上标到下标,也就是反着取
   begin
       ResultStr := ResultStr + tmpChar[I];  //以字符串的形式返回结果
   end;
   Result := ResultStr;
end;

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.