贴一个将数字金额转成英文大写金额的函数

EdwinYeah 2001-07-28 04:14:16
可将¥123.45转成 ONE HUNDRED AND TWEENTY-THREE AND FORTY-FIVE CENTS ONLY

是由以前写的VB代码直接翻译过来的,可能不好看,并且有诸多限制,谁写一个万能的贴上来吧


//将在0.01-99,999999.99之间的数字转换成英文表示法
//调用本Function时,strDigits不可包含千位分隔符,并且小数部分要保留两位,如果有的话。
function DigitToEn(strDigits: String; intRMB: Boolean): String;
//将在1-19之间的数字转换成英文表示法
function DigitToEn1(strDigits: String): String;
begin
Case StrToInt(strDigits) of
1: Result := 'ONE';
2: Result := 'TWO';
3: Result := 'THREE';
4: Result := 'FOUR';
5: Result := 'FIVE';
6: Result := 'SIX';
7: Result := 'SEVEN';
8: Result := 'EIGHT';
9: Result := 'NINE';
10: Result := 'TEN';
11: Result := 'ELEVEN';
12: Result := 'TWELVE';
13: Result := 'THIRTEEN';
14: Result := 'FOURTEEN';
15: Result := 'FifTEEN';
16: Result := 'SIXTEEN';
17: Result := 'SEVENTEEN';
18: Result := 'EIGHTEEN';
19: Result := 'NINETEEN';
else
Result := '';
end;
end;


//将在1-99之间的数字转换成英文表示法
function DigitToEn2(strDigits: String): String;
var
strTemp: String;
begin
if StrToInt(strDigits) < 20 then
Result := DigitToEn1(strDigits)
else begin
Case StrToInt(Left(strDigits, 1)) of
2: strTemp := 'TWENTY';
3: strTemp := 'THIRTY';
4: strTemp := 'FORTY';
5: strTemp := 'FIFTY';
6: strTemp := 'SIXTY';
7: strTemp := 'SEVENTY';
8: strTemp := 'EIGHTY';
9: strTemp := 'NINETY';
else
strTemp := '';
end;

if Right(strDigits, 1) <> '0' then
strTemp := strTemp + '-' + DigitToEn1(Right(strDigits, 1));

Result := strTemp;
end;
end;


//将在1-999之间的数字转换成英文表示法
//如intFormat为1,则在HUNDRED 后面就有 AND,否则没有。
function DigitToEn3(strDigits: String; intFormat: Integer): String;
begin
//去掉数字串前面的0
strDigits := IntToStr(StrToInt(strDigits));

if StrToFloat(strDigits) <= 19 then
Result := DigitToEn1(strDigits)
else if (StrToFloat(strDigits) >= 20) and (StrToFloat(strDigits) <= 99) then
Result := DigitToEn2(strDigits)
else begin
Result := DigitToEn1(Left(strDigits, 1)) + ' HUNDRED';
if (StrToFloat(Right(strDigits, 2)) > 0) and
(StrToFloat(Right(strDigits, 2)) < 20) then
if intFormat = 1 then
Result := Result + ' AND ' + DigitToEn1(Right(strDigits, 2))
else
Result := Result + ' ' + DigitToEn1(Right(strDigits, 2))
else if (StrToFloat(Right(strDigits, 2)) >= 20)
and (StrToFloat(Right(strDigits, 2)) <= 99) then
if intFormat = 1 then
Result := Result + ' AND ' + DigitToEn2(Right(strDigits, 2))
else
Result := Result + ' ' + DigitToEn2(Right(strDigits, 2));
end;
end;

var
// 整数部份 小数部份
strInteger, strDecimal: String;
strTemp: String;
begin
if (StrToFloat(strDigits) > 99999999.99) or
(StrToFloat(strDigits) < 0.01) then
raise exception.Create('Out of range, must between 0.01 - 99999999.99');

//有整数部分及小数部分
if (Int(StrToFloat(strDigits)) > 0) and
(StrToFloat(strDigits) - Int(StrToFloat(strDigits)) > 0) then
begin
strInteger := Left(strDigits, Pos('.', strDigits) - 1);
strDecimal := Right(strDigits, Length(strDigits) - Pos('.', strDigits));
end
//只有整数部分
else if Int(StrToFloat(strDigits)) > 0 then
strInteger := IntToStr(Round(StrToFloat(strDigits)))
//只有小数部分
else if StrToFloat(strDigits) - Int(StrToFloat(strDigits)) > 0 then
strDecimal := Right(strDigits, Length(strDigits) - Pos('.', strDigits));

//得到整数部分英文表示法
if strInteger <> '' then begin
strTemp := DigitToEn3(Right(strInteger, 3), 1);
if Length(strInteger) > 3 then begin
strTemp := DigitToEn3(Left(Right(strInteger, 6),
Length(Right(strInteger, 6)) - 3), 2) + ' THOUSAND ' + strTemp;
if Length(strInteger) > 6 then
strTemp := DigitToEn3(Left(strInteger, Length(strInteger) - 6), 2) +
' MILLION ' + strTemp;
end;
strInteger := strTemp;
end;

if (strDecimal <> '') and (Length(strDecimal) <= 3) then
strDecimal := DigitToEn3(strDecimal, 1);

if (strInteger <> '') and (strDecimal <> '') then
if intRMB then
Result := strInteger + ' YUANS AND ' + strDecimal + ' CENTS ONLY'
else
Result := strInteger + ' DOLLARS AND ' + strDecimal + ' CENTS ONLY'
else if strInteger <> '' then
if intRMB then
Result := strInteger + ' YUANS ONLY'
else
Result := strInteger + ' DOLLARS ONLY'
else if strDecimal <> '' then
Result := strDecimal + ' CENTS ONLY'
else
Result := 'ZERO';
end;
...全文
474 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hssfox 2001-11-20
  • 打赏
  • 举报
回复
看看
EdwinYeah 2001-07-28
  • 打赏
  • 举报
回复
怎麼沒用?我公司的發票就要用到,因為是面向香港客戶的.
vince12 2001-07-28
  • 打赏
  • 举报
回复
有用吗?

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧