# 红鱼儿

### 统计

• 随笔 - 200
• 文章 - 1
• 评论 - 53
• 引用 - 0
unit TU2.Helper.Currency;

interface

function CurrencyToChineseCapitalCharacter(const AValue: Currency; const ADecimals: Cardinal=4): string;
function CurrencyToString(const AValue: Currency; const ADecimals: Cardinal=4): string;

implementation

uses System.SysUtils, System.Math;

function CurrencyRound(var U: UInt64; const ADecimals: Cardinal): Integer; inline;
var
W: UInt64;
begin//Bankers-rounding
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;
Result := UIntToStr(U);
if Digits<4 then
Result := Result.Insert(Result.Length+Digits-4, DecimalDotChar);
if Negative then
Result := NegativeChar + Result;
end;

end.

1. 阿拉伯金额数字中间有“0”时，人民币大写要写“零”字。如￥1409.50，应写成人民币陆壹仟肆佰零玖元伍角。
2. 阿拉伯金额数字中间连续有几个“0”时，人民币大写金额中间可以只写一个“零”字。如￥6007.14，应写成人民币陆仟零柒元壹角肆分。
3. 阿拉伯金额数字万位和元位是“0”；或者数字中间连续有几个“0”，万位（或元位）也是“0”，但千位（或角位）不是“0”时；中文大写金额中可以只写一个零字，也可以不写“零”字。如￥1680.32，应写成人民币壹仟陆佰捌拾元零叁角贰分或者写成人民币壹仟陆佰捌拾元叁角贰分。又如￥107000.53，应写成人民币壹拾万柒仟元零伍角叁分或者写成人民币壹拾万零柒仟元伍角叁分。
4. 阿拉伯金额数字角位是“0”，而分位不是“0”时，中文大写金额“元”后面应写“零”字。如￥16409.02，应写成人民币壹万陆仟肆佰零玖元零贰分，又如￥325.04.应写成人民币叁佰贰拾伍元零肆分。
————————————————

posted on 2019-09-12 10:31  红鱼儿  阅读(...)  评论(...编辑  收藏