大写金额转为英文的算法 修改版
从网上找到了这个算法,原作是Dylan Thomas,但是我使用后发现有少少的缺点,例如英文金额的习惯是小数用Cents Twenty-Five这样的表示,但原版中没有小数的功能,只是简单的用百分数表示了。
另外十位的部分也有少许欠缺,25应为:Twenty-Five,再其次就是增加了0到1之间的小数的表示。我在网上搜了一下发现似乎没人修正这些问题,特此发贴与大家分享:
- {**********}
- { Number to letters unit version 1.2 }
- { copyright (C) Dylan Thomas 2000 }
- { License: No significant restrictions. }
- { Language: US. English }
- { Last Edit By logitech1229 2006. }
- unit NumberToLetters;
- interface
- (* This function returns the written equivalent of a number. *)
- function NumToLetters(Number: Real): string;
- implementation
- uses SysUtils;
- type
- TNumberStr = string[13];
- const
- Numbers: array[1..19] of TNumberStr = ('One','Two','Three', 'Four',
- 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve',
- 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen',
- 'Nineteen');
- Tenths: array[1..9] of TNumberStr = ('Ten', 'Twenty', 'Thirty', 'Forty',
- 'Fifty', 'Sixty', 'Seventy','Eighty','Ninety');
- ErrorString = 'not in valid range';
- Min = 1.00;
- Max = 4294967295.99;
- function NumToLetters(Number: Real): string;
- var Dec:String;
- function RecurseNumber(N: LongWord): string;
- begin
- case N of
- 1..19:
- Result := Numbers[N];
- 20..99:
- begin
- if (N in [20,30,40,50,60,70,80,90]) then Result := Tenths[N div 10]
- else Result := Tenths[N div 10] +'-'+ RecurseNumber(N mod 10);
- end;
- 100..999:
- Result := Numbers[N div 100] + ' Hundred And ' + RecurseNumber(N mod 100);
- 1000..999999:
- Result := RecurseNumber(N div 1000) + ' Thousand, ' +
- RecurseNumber(N mod 1000);
- 1000000..999999999: Result := RecurseNumber(N div 1000000) + ' Million, '
- + RecurseNumber(N mod 1000000);
- 1000000000..4294967295: Result := RecurseNumber(N div 1000000000) +
- ' Billion, ' + RecurseNumber(N mod 1000000000);
- end; {Case N of}
- end; {RecurseNumber}
- begin
- {Calls := 0;}
- if (Number >= Min) and (Number <= Max) then
- begin
- Result := RecurseNumber(Round(Int(Number)));
- {Added for cents in a currency value}
- if not(Frac(Number) = 0.00) then
- {write like 'cents Twenty-five',If you want a result like 'Nought Point two five',you can use strings in {}}
- Result :=Result + ' And Cents '+ RecurseNumber(Round(Frac(Number)*100))
- {Result :=Result +' Point '+ RecurseNumber(Round(Int(Frac(Number)*10)))+' '+RecurseNumber(Round(Int(Frac(Number*10)*10)))}
- else Result := Result +' Only';
- end;
- if (Number >0) and (Number<Min) then
- begin
- if not(Frac(Number) = 0.00) then
- {write like 'cents Twenty-five',If you want a result like 'Nought Point two five',you can use strings in {}}
- Result :=Result + ' Cents '+ RecurseNumber(Round(Frac(Number)*100));
- {Result :=Result +' Point '+ RecurseNumber(Round(Int(Frac(Number)*10)))+' '+RecurseNumber(Round(Int(Frac(Number*10)*10)))}
- end;
- end;{NumToLetters}
- end.
-------------------------------------------------------------------------------
- function NumToChar(const n: Real): string; //可以到万亿,并且可以随便扩大范围
- const cNum: WideString = '零壹贰叁肆伍陆柒捌玖--万仟佰拾亿仟佰拾万仟佰拾元角分';
- cCha:array[0..1, 0..12]of string =
- (( '零元','零拾','零佰','零仟','零万','零亿','亿万','零零零','零零','零万','零亿','亿万','零元'),
- ( '元','零','零','零','万','亿','亿','零','零','万','亿','亿','元'));
- var i : Integer;
- sNum,sTemp : WideString;
- begin
- result :='';
- sNum := format('%15d',[round(n * 100)]);
- for i := 0 to 14 do
- begin
- stemp := copy(snum,i+1,1);
- if stemp=' ' then continue
- else result := result + cNum[strtoint(stemp)+1] + cNum[i+13];
- end;
- for i:= 0 to 12 do
- Result := StringReplace(Result, cCha[0,i], cCha[1,i], [rfReplaceAll]);
- if pos('零分',result)=0
- then Result := StringReplace(Result, '零角', '零', [rfReplaceAll])
- else Result := StringReplace(Result, '零角','整', [rfReplaceAll]);
- Result := StringReplace(Result, '零分','', [rfReplaceAll]);
- end;
-------------------------------------------------------------------------------
- function Changdx2(mmje: Double): String;
- const s1: String = '零壹贰叁肆伍陆柒捌玖';
- s2: String = '分角元拾佰仟万拾佰仟亿拾佰仟万';
- function StrTran(const S, S1, S2: String): String;
- begin
- Result := StringReplace(S, S1, S2, [rfReplaceAll]);
- end;
- var s, dx: String;
- i, Len: Integer;
- begin
- if mmje < 0 then
- begin dx := '负';
- mmje := -mmje;
- end;
- s := Format('%.0f', [mmje*100]);
- Len := Length(s);
- for i := 1 to Len do
- dx := dx + Copy(s1, (Ord(s[i]) - ord('0'))*2 + 1, 2) + Copy(s2, (Len - i)*2 + 1, 2);
- dx := StrTran(StrTran(StrTran(StrTran(StrTran(dx, '零仟', '零'), '零佰', '零'), '零拾', '零'), '零角', '零'), '零分', '整');
- dx := StrTran(StrTran(StrTran(StrTran(StrTran(dx, '零零', '零'), '零零', '零'), '零亿', '亿'), '零万', '万'), '零元', '元');
- if dx = '整' then Result := '零元整'
- else Result := StrTran(StrTran(dx, '亿万', '亿零'), '零整', '整');
- end;
--------------------------------------------------------------------------------
- public static String numtochinese(String input){
- String s1="零壹贰叁肆伍陆柒捌玖";
- String s4="分角整元拾佰仟万拾佰仟亿拾佰仟";
- String temp="";
- String result="";
- if (input==null) return "输入字串不是数字串只能包括以下字符(´0´~´9´,´.´),输入字串最大只能精确到仟亿,小数点只能两位!";
- temp=input.trim();
- float f;
- try{
- f=Float.parseFloat(temp);
- }catch(Exception e){return "输入字串不是数字串只能包括以下字符(´0´~´9´,´.´),输入字串最大只能精确到仟亿,小数点只能两位!";}
- int len=0;
- if (temp.indexOf(".")==-1) len=temp.length();
- else len=temp.indexOf(".");
- if(len>s4.length()-3) return("输入字串最大只能精确到仟亿,小数点只能两位!");
- int n1,n2=0;
- String num="";
- String unit="";
- for(int i=0;iif(i>len+2){break;}
- if(i==len) {continue;}
- n1=Integer.parseInt(String.valueOf(temp.charAt(i)));
- num=s1.substring(n1,n1+1);
- n1=len-i+2;
- unit=s4.substring(n1,n1+1);
- result=result.concat(num).concat(unit);
- }
- if ((len==temp.length()) ¦ ¦(len==temp.length()-1)) result=result.concat("整");
- if (len==temp.length()-2) result=result.concat("零分");
- return result;
- }
--------------------------------------------------------------------------------
- //因为找不到相关函数,便自己写了一个,是仿Excel 金额转换,不限金额长度。
- const NumberArray: array[0..9] of string = ('零', '壹','貳','叁','肆','伍','陆','柒','捌','玖');
- // 数字转与大写
- function GetMoneySwitch(AMoney: string): string;
- // 去除所有分隔符
- procedure ClearComma(var AValue: string);
- begin
- while Pos(',', AValue) > 0 do
- Delete(AValue, Pos(',', AValue), 1);
- end;
- // 测试如果为零将不返回值
- function FiltrateValue(const AValue, AStr: string): string;
- var
- IntValue: Integer;
- begin
- IntValue:= StrToIntDef(AValue, 0);
- if IntValue > 0 then Result:= AStr;
- end;
- // 直接将数字翻译成大写
- function Direct(const AValue: string): string;
- var
- ResultStr: string;
- iCount: Integer;
- begin
- for iCount:= 1 to Length(AValue) do
- ResultStr:= ResultStr + NumberArray[StrToInt(AValue[iCount])];
- Result:= ResultStr;
- end;
- // 将四位长度的数字翻译与大写
- function FourBit(const AValue: string): string;
- var
- i, x, j: Integer;
- IntValue: Integer;
- ResultStr: string;
- begin
- IntValue:= StrToIntDef(AValue, 0);
- x:= IntValue;
- i := x div 1000;
- j := x mod 1000;
- if i <> 0 then ResultStr:= NumberArray[i] + '仟'
- else begin
- if Length(AValue) > 3 then ResultStr:= '零';
- end;
- i := j div 100;
- j := j mod 100;
- if i <> 0 then ResultStr:= ResultStr + NumberArray[i] + '佰'
- else begin
- if (ResultStr <> '') and (Length(AValue) > 2) and
- (Copy(ResultStr, Length(ResultStr)-1, 2) <> '零') then
- ResultStr:= ResultStr + '零';
- end;
- i := j div 10;
- j := j mod 10;
- if i <> 0 then ResultStr := ResultStr + NumberArray[i] + '拾'
- else begin
- if (ResultStr <> '') and (Length(AValue) > 1) and
- (Copy(ResultStr, Length(ResultStr)-1, 2) <> '零') then
- ResultStr:= ResultStr + '零';
- end;
- ResultStr := ResultStr + NumberArray[j];
- while Copy(ResultStr, Length(ResultStr)-1, 2) = '零' do
- Delete(ResultStr, Length(ResultStr)-1, 2);
- Result := ResultStr;
- end;
- var
- IntegerValue: string; // 整数部分的值
- KilomegaValue: string; // 存储大于千兆的数字
- AccountValue: string; // 在千兆以内的整数部分
- DecimalValue: string; // 存在小数点后的值
- ResultKilomega: string; // 大于千兆并翻译后的大写字符
- ResultAccount: string; // 在千兆以内的整数部分并翻译后的大写字符
- ResultDecimal: string; // 小数点后的值并翻译后的大写字符
- FourBitStr: string; // 最大四位值的字符
- begin
- // 清除分隔符
- ClearComma(AMoney);
- // 验证字符串是否合法
- try
- AMoney:= FloatToStr(StrToFloat(AMoney));
- except
- raise Exception.Create('无效的数值字符串');
- end;
- // 取到小数据点后的值
- // 取出整数部分的值
- if Pos('.', AMoney) > 0 then
- begin
- DecimalValue:= Copy(AMoney, Pos('.', AMoney) + 1, Length(AMoney));
- IntegerValue:= Copy(AMoney, 0, Pos('.', AMoney)-1);
- ResultDecimal:= '.' + Direct(DecimalValue);
- end
- else IntegerValue:= AMoney;
- // 取到大于千兆的数字
- // 取到在千兆以内的整数部分
- if Length(IntegerValue) > 16 then
- begin
- KilomegaValue:= Copy(IntegerValue, 0, Length(IntegerValue) - 12);
- AccountValue:= Copy(IntegerValue,
- Length(IntegerValue) - 11, Length(IntegerValue));
- ResultKilomega:= Direct(KilomegaValue) + '兆';
- end
- else AccountValue:= IntegerValue;
- { 翻译在千兆以内的整数部分 }
- // 翻译在兆与仟兆之间的部份
- if Length(AccountValue) > 12 then
- begin
- FourBitStr:= Copy(AccountValue, 0, Length(AccountValue) - 12);
- ResultAccount:= ResultAccount +
- FourBit(FourBitStr) + FiltrateValue(FourBitStr, '兆');
- Delete(AccountValue, 1, Length(AccountValue) - 12);
- end;
- // 翻译在亿与仟亿之间的部份
- if Length(AccountValue) >= 8 then
- begin
- FourBitStr:= Copy(AccountValue, 0, Length(AccountValue) - 8);
- ResultAccount:= ResultAccount +
- FourBit(FourBitStr) + FiltrateValue(FourBitStr, '亿');
- Delete(AccountValue, 1, Length(AccountValue) - 8);
- end;
- // 翻译在万与仟万之间的部份
- if Length(AccountValue) >= 5 then
- begin
- FourBitStr:= Copy(AccountValue, 0, Length(AccountValue) - 4);
- ResultAccount:= ResultAccount +
- FourBit(FourBitStr) + FiltrateValue(FourBitStr, '万');
- Delete(AccountValue, 1, Length(AccountValue) - 4);
- end;
- // 翻译万以下的部份
- if Length(AccountValue) > 0 then
- begin
- ResultAccount:= ResultAccount +
- FourBit(Copy(AccountValue, 0, Length(AccountValue)));
- end;
- // 组合字符串
- Result:= ResultKilomega + ResultAccount + ResultDecimal;
- end;