Delphi货币类型转中文大写金额

来源:互联网 发布:淘宝国乐一号 编辑:程序博客网 时间:2024/06/05 18:30

在Delphi中,为了实现货币数值运算中的严格精度要求,内部把货币类型数据当作一个放大10000倍的64位整数来处理。这样根据64位整数的范围,可以得出货币类型Currency的范围是 [-922337203685477.5807; 922337203685477.5807]。

货币类型一个最常见的应用场景是金额大写转换,网上都是一些先将货币转字符串后再对字符串处理的代码,而且有些方法在有些情况下不满足金额大写规范,这里给出一个直接转换的方法。

unit TU2.Helper.Currency;interfacefunction CurrencyToChineseCapitalCharacter(const AValue: Currency; const ADecimals: Cardinal=4): string;function CurrencyToString(const AValue: Currency; const ADecimals: Cardinal=4): string;implementationuses System.SysUtils, System.Math;function CurrencyRound(var U: UInt64; const ADecimals: Cardinal): Integer; inline;var  W: UInt64;begin//Bankers-rounding  Result := 4-ADecimals;  if Result<0 then    Result := 0  else if Result>0 then  begin    case Result of      1:begin   //li        DivMod(U, 10, U, W);        if (W > 5) or ((W = 5) and Odd(U)) then          Inc(U);      end;      2:begin  //fen        DivMod(U, 100, U, W);        if (W > 50) or ((W = 50) and Odd(U)) then          Inc(U);      end;      3:begin  //jiao        DivMod(U, 1000, U, W);        if (W > 500) or ((W = 500) and Odd(U)) then          Inc(U);      end;      4:begin  //yuan        DivMod(U, 10000, U, W);        if (W > 5000) or ((W = 5000) and Odd(U)) then          Inc(U);      end;    end;  end;end;function CurrencyToChineseCapitalCharacter(const AValue: Currency; const ADecimals: Cardinal=4): string;const//Currency: [-922337203685477.5807, 922337203685477.5807]  CCCNegative = '负';  CCCZheng = '整';  CCCNumbers: array[0..9] of Char = ('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');  CCCUnits: array[0..18] of Char = ('毫', '厘', '分', '角', '元','拾','佰','仟','万',                                     '拾','佰','仟','亿','拾','佰','仟','万','兆','拾');var  U, W: UInt64;  Digits, Idx, ZeroFlag: Integer;  Negative: Boolean;  Buff: array[0..38] of Char;begin  U := PUInt64(@AValue)^;  if U <> 0 then  begin    Negative := (U and $8000000000000000) <> 0;    if Negative then      U := not U + 1;    Digits := CurrencyRound(U, ADecimals);    if U<>0 then    begin      //Try skip trailing zero      repeat        DivMod(U, 10, U, W);        Inc(Digits);      until W<>0;      Dec(Digits);      Idx := 38;      if Digits>=3 then      begin        Buff[Idx] := CCCZheng;        Dec(Idx);        if Digits>4 then        begin          Buff[Idx] := CCCUnits[4];          Dec(Idx);          if Digits>17 then          begin            Buff[Idx] := CCCUnits[17];            Dec(Idx);          end else if Digits>12 then          begin            Buff[Idx] := CCCUnits[12];            Dec(Idx);          end else if Digits>8 then          begin            Buff[Idx] := CCCUnits[8];            Dec(Idx);          end;        end;      end;      Buff[Idx] := CCCUnits[Digits];      Dec(Idx);      Buff[Idx] := CCCNumbers[W];      Dec(Idx);      //Do Split      ZeroFlag := 0;      while U<>0 do      begin        Inc(Digits);        DivMod(U, 10, U, W);        if Digits in [4,8,12,17] then        begin          if ZeroFlag>0 then          begin            Buff[Idx] := CCCNumbers[0];            Dec(Idx);          end else if (ZeroFlag<0) and (Digits>8) then            Inc(Idx);          Buff[Idx] := CCCUnits[Digits];          Dec(Idx);          if W<>0 then          begin            Buff[Idx] := CCCNumbers[W];            Dec(Idx);            ZeroFlag := 0;          end else            ZeroFlag := -1;        end else begin          if W<>0 then          begin            if ZeroFlag>0 then            begin              Buff[Idx] := CCCNumbers[0];              Dec(Idx);            end;            Buff[Idx] := CCCUnits[Digits];            Dec(Idx);            Buff[Idx] := CCCNumbers[W];            Dec(Idx);            ZeroFlag := 0;          end else begin            if ZeroFlag=0 then              ZeroFlag := 1;          end;        end;      end;      if Negative then        Buff[Idx] := CCCNegative      else Inc(Idx);      //Copy Result      Digits := 38+1-idx;      SetLength(Result, Digits);      Move(Buff[idx], PChar(Result)^, Digits * SizeOf(WideChar));      Exit;    end;  end;  Result := CCCNumbers[0]+CCCUnits[4]+CCCZheng;end;function CurrencyToString(const AValue: Currency; const ADecimals: Cardinal=4): string;const  NegativeChar = '-';  DecimalDotChar = '.';var  U: UInt64;  Digits: Integer;  Negative: Boolean;begin  U := PUInt64(@AValue)^;  Negative := (U and $8000000000000000) <> 0;  if Negative then    U := not U + 1;  Digits := CurrencyRound(U, ADecimals);  Result := UIntToStr(U);  if Digits<4 then    Result := Result.Insert(Result.Length+Digits-4, DecimalDotChar);  if Negative then    Result := NegativeChar + Result;end;end.


对上面两个方法进行最大货币数值测试,并与自带货币转字符串方法比较。

64位结果如下:


32位结果如下:


注:耗时采用高精度计时器测量,应忽略突兀数值看平均值。


从对比上可以看出在64位程序下,这个方法性能要好于系统自带转换方法,而且其中文大写转换跟字符串转换性能一样。但在32位系统下,性能却差一点,这是因为这个方法主要采用了DivMod方法进行整除取余操作获取每位的数字,该方法在32位程序下,当被除数大于32位正整数时,其采用循环减10实现整除取余,这种情况下循环次数非常大。


附: 金额大写规范

一、人民币大写金额数字到“元”为止的,在“元”之后,应写“整”(或“正”)字;在“角”之后,可以不写“整”(或“正”)字;大写金额数字有“分”的,“分”后面不写“整”(或“正”)字。
二、阿拉伯数字小写金额数字中有“0”时,人民币大写应按照汉语语言规律。举例如下:
1. 阿拉伯金额数字中间有“0”时,人民币大写要写“零”字。如¥1409.50,应写成人民币陆壹仟肆佰零玖元伍角。
2. 阿拉伯金额数字中间连续有几个“0”时,人民币大写金额中间可以只写一个“零”字。如¥6007.14,应写成人民币陆仟零柒元壹角肆分。
3. 阿拉伯金额数字万位和元位是“0”;或者数字中间连续有几个“0”,万位(或元位)也是“0”,但千位(或角位)不是“0”时;中文大写金额中可以只写一个零字,也可以不写“零”字。如¥1680.32,应写成人民币壹仟陆佰捌拾元零叁角贰分或者写成人民币壹仟陆佰捌拾元叁角贰分。又如¥107000.53,应写成人民币壹拾万柒仟元零伍角叁分或者写成人民币壹拾万零柒仟元伍角叁分。
4. 阿拉伯金额数字角位是“0”,而分位不是“0”时,中文大写金额“元”后面应写“零”字。如¥16409.02,应写成人民币壹万陆仟肆佰零玖元零贰分,又如¥325.04.应写成人民币叁佰贰拾伍元零肆分。


原创粉丝点击