FastReport 金额转大写

来源:互联网 发布:澜本嫁衣 知乎 编辑:程序博客网 时间:2024/04/28 17:24
function atoc(a: string): string;
var
  string1, string2, string3, string4, ch1, ch2, s, s1, rmb: string;
  i, j, k, zero: Integer;
begin
  string1 := '零壹贰叁肆伍陆柒捌玖';
  string2 := '佰拾万仟佰拾亿仟佰拾万仟佰拾元角分';
  s1 := '';
  zero := 0;
  rmb := '';
  try
    s := trim(str);
    s := format('%.2f', [strtofloat(str)]);
    s := Trim(FloatToStr(strtofloat(str) * 100)); // '将数字转成整型字符串
    j := Length(s); // '字符串长度
    string4 := copy(string2, Length(string2) - j * 2 + 1, j * 2); // '取出所需最大位数的表示
    for i := 1 to j do
    begin
      string3 := copy(s, i, 1);
      ch1 := copy(string1, (StrToInt(string3) + 1) * 2 - 1, 2);
      ch2 := copy(string4, i * 2 - 1, 2);
      if string3 <> '0' then
        zero := 0
      else
      begin
        zero := zero + 1;
        if (j - i <> 14) and (j - i <> 10) and (j - i <> 6) and (j - i <> 2) or (zero > 3) and (j - i = 6) then
          ch2 := '';
        if (copy(s, i, 2) = '00') or (string3 = '0') and (j - i = 14) or (j - i = 10) or (j - i = 6) or (j - i = 2) or (j - i = 0) then
          ch1 := '';
      end;
      rmb := rmb + ch1 + ch2;
      if (j - i = 14) or (j - i = 10) or (j - i = 6) or (j - i = 2) then
        if (copy(s, i, 1) = '0') and (copy(s, i + 1, 1) <> '0') then
          rmb := rmb + '零';
    end;
    if string3 = '0' then
      rmb := rmb + '整';
    Result := rmb;
  except
    Result := '';
  end;
end;
0 0