大写金额转为英文的算法NumberToLetters修改版By Logitech1229[分享](50分)

  • 主题发起人 主题发起人 logitech1229
  • 开始时间 开始时间
L

logitech1229

Unregistered / Unconfirmed
GUEST, unregistred user!
说明:从网上找到了这个算法,原作是Dylan Thomas,但是我使用后发现有少少的缺点,例如英文金额的习惯是小数用Cents Twenty-Five这样的表示,但原版中没有小数的功能,只是简单的用百分数表示了。香港习惯的金额写法中只有百位的Hundred and Twenty-Five这样的写法,到了千位之后的就不要and,例如:Two hundred Fifty-two Thousand Two Hundred and Fifty-Two 。这个也做到了
另外十位的部分也有少许欠缺,25应为:Twenty-Five,再其次就是增加了0到1之间的小数的表示。我在网上搜了一下发现似乎没人修正这些问题,特此发贴与大家分享[:D][:D]
BTW:我不知道为什么Max = 4294967295.99,50分送给能解答此问题的朋友,嘿嘿

{**************************************************}
{ }
{ 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;level:Boolean) : 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),True);
end;

100..999:
begin
if level=True then
begin
if (N=100)or (N=200)or (N=300) or (N=400) or (N=500)or (N=600)or (N=700) or (N=800) or (N=900) then Result := RecurseNumber((N div 100),True)+' Hundred '
else Result := Numbers[N div 100] + ' Hundred And ' + RecurseNumber((N mod 100),True);
end
else
begin
if (N=100)or (N=200)or (N=300) or (N=400) or (N=500)or (N=600)or (N=700) or (N=800) or (N=900) then Result := RecurseNumber((N div 100),True)+' Hundred '
else Result := Numbers[N div 100] + ' Hundred ' + RecurseNumber((N mod 100),True);
end;
end;

1000..999999:
Result := RecurseNumber((N div 1000),False)+ ' Thousand ' +
RecurseNumber((N mod 1000),True);
1000000..999999999: Result := RecurseNumber((N div 1000000),False) + ' Million '
+ RecurseNumber((N mod 1000000),True);
1000000000..4294967295: Result := RecurseNumber((N div 1000000000),False) +
' Billion, ' + RecurseNumber((N mod 1000000000),True);
end; {Case N of}
end; {RecurseNumber}

begin
{Calls := 0;}
if (Number >= Min) and (Number <= Max) then
begin
Result := RecurseNumber(Round(Int(Number)),True);
{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),True)
{Result :=Result +' Point '+ RecurseNumber(Round(Int(Frac(Number)*10)),True)+' '+RecurseNumber(Round(Int(Frac(Number*10)*10)),True)}
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),True);
{Result :=Result +' Point '+ RecurseNumber(Round(Int(Frac(Number)*10)),True)+' '+RecurseNumber(Round(Int(Frac(Number*10)*10)),True)}
end;
end;{NumToLetters}

end.
 
蓝叶子收藏了。
 

Similar threads

I
回复
0
查看
487
import
I
I
回复
0
查看
762
import
I
I
回复
0
查看
687
import
I
I
回复
0
查看
2K
import
I
I
回复
0
查看
1K
import
I
后退
顶部