那位大俠把下述C源碼轉為delphi,下述源碼為數字金額轉泰文。
public static String ThaiBahtText(String strNumber, boolean IsTrillion) throws Exception{String BahtText = "";String strTrillion = "";String[] strThaiNumber = { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า", "สิบ" };String[] strThaiPos = { "", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน", "ล้าน" }; String strInteger = ***.split(".")[0];String strSatang = "00";if(***.split(".").length==1){strSatang = "00";}if(***.split(".").length>1){if(***.split(".")[1].length() < 2){strSatang=***.split(".")[1]+"0";}else{strSatang=***.split(".")[1];}} if (***.length() > 13) throw new Exception("รองรับตัวเลขได้เพียง ล้านล้าน เท่านั้น!"); boolean _IsTrillion = ***.length() > 7 ; if (_IsTrillion) { system.***.println(***.length()); strTrillion = ***.substring(0, ***.length() - 6); system.***.println(strTrillion); BahtText = ThaiBahtText(strTrillion, _IsTrillion); strInteger = ***.substring(***.length()); } int strLength = ***.length(); for (int i = 0; i < ***.length(); i++) { String number = ***.substring(i, (i+1)); if (!"0".equals(number)) { if (i == strLength - 1 && "1".equals(number) && strLength != 1) { BahtText += "เอ็ด"; } else if (i == strLength - 2 && "2".equals(number) && strLength != 1) { BahtText += "ยี่"; } else if (i != strLength - 2 || !"1".equals(number)) { BahtText += strThaiNumber[***.parseint(number)]; } BahtText += strThaiPos[(strLength - i) - 1]; } } if (IsTrillion){return BahtText + "ล้าน";} if(!"0".equals(strInteger)) { BahtText += "บาท"; } if ("00".equals(strSatang)) { BahtText += "ถ้วน"; }else { strLength = ***.length(); for (int i = 0; i < ***.length(); i++) { String number = ***.substring(i, (i+1)); if (!"0".equals(number)) { if (i == strLength - 1 && "1".equals(number) && !"0".equals(***.substring(0,1))) { BahtText += "เอ็ด"; } else if (i == strLength - 2 && "2".equals(number) && !"0".equals(***.substring(0,1))) { BahtText += "ยี่"; } else if (i != strLength - 2 || !"1".equals(number)) { BahtText += strThaiNumber[***.parseint(number)]; } BahtText += strThaiPos[(strLength - i) - 1]; } } BahtText += "สตางค์"; }return BahtText;}
你的程序根本不是C的,是java的,改为delphi没有任何难度,只是要化功夫
二小时搞定,没有泰文环境(也不想安装它了),你自己测试下(注意字符集)
uses ***.strutils, ***.sysutils;
{ uses StrUtils,SysUtils; //低版本用}
function ThaiBahtText(strNumber: String; IsTrillion: Boolean): String;
const
strThaiNumber: Array [0 .. 10] of String = ('ศูนย์', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า', 'สิบ');
strThaiPos: Array [0 .. 6] of String = ('', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน');
var
BahtText, strTrillion, strInteger, strSatang, number: String;
_IsTrillion: Boolean;
strLength, i: integer;
begin
BahtText := '';
strTrillion := '';
strInteger := SplitString(strNumber, '.')[0];
strSatang := '00';
if Length(SplitString(strNumber, '.')) = 1 then
begin
strSatang := '00';
end;
if Length(SplitString(strNumber, '.')) > 1 then
begin
if Length(SplitString(strNumber, '.')) < 2 then
begin
strSatang := SplitString(strNumber, '.')[1] + '0';
end
else
begin
strSatang := SplitString(strNumber, '.')[1]
end;
end;
if (Length(strInteger) > 13) then
raise ***.create ('รองรับตัวเลขได้เพียง ล้านล้าน เท่านั้น!') ;
if Length(strInteger) > 7 then
_IsTrillion := true
else
_IsTrillion := false;
if _IsTrillion then
begin
writeln(Length(strInteger));
strTrillion := copy(strInteger, 1, Length(strInteger) - 5);
writeln(strTrillion);
BahtText := ThaiBahtText(strTrillion, _IsTrillion);
strInteger := copy(strInteger, 1, Length(strTrillion));
end;
strLength := Length(strInteger);
for i := 0 to Length(strInteger)-1 do
begin
number := copy(strInteger, i+1, 1);
if not ('0' = number) then
begin
if (i = strLength - 1) and ('1'=number) and (strLength <> 1) then
begin
BahtText :=BahtText+ 'เอ็ด';
end
else if (i = strLength - 2 ) and ('2'= number) and ( strLength <> 1) then
begin
BahtText := BahtText+'ยี่';
end else
if (i <> strLength - 2) or not ('1' = number) then
begin
BahtText := BahtText+strThaiNumber[StrtoInt(number)];
end;
BahtText := BahtText+strThaiPos[(strLength - i) - 1];
end;
end;
if IsTrillion then begin
result:= BahtText + 'ล้าน';
exit
end;
if not ('0'=strInteger) then
begin
BahtText :=BahtText + 'บาท';
end;
if '00'=strSatang then
begin
BahtText := BahtText+'ถ้วน';
end else
begin
strLength := length(strSatang);
for i := 0 to length(strSatang)-1 do
begin
number := copy(strSatang,i+1, 1);
if not ('0'=number) then
begin
if (i = strLength - 1) and ('1'=number) and (not ('0'=copy(strSatang,1, 1))) then
begin
BahtText := BahtText+'เอ็ด';
end else
if (i = strLength - 2) and ('2'=number) and (not ('0'=copy(strSatang,1, 1))) then
begin
BahtText := BahtText+'ยี่';
end else
if (i <> strLength - 2) or (not ('1'=number)) then
begin
BahtText := BahtText+strThaiNumber[strtoint(number)];
end;
BahtText := BahtText+strThaiPos[(strLength - i) - 1];
end;
end;
BahtText := BahtText+'สตางค์';
end;
result:= BahtText;
end;
begin //测试
writeln(ThaiBahtText('123.45', false));
end.
下一篇:这个IF函数怎么编辑