數字轉中文大寫 程式 |
|
it1506
初階會員 ![]() ![]() 發表:33 回覆:89 積分:49 註冊:2011-02-16 發送簡訊給我 |
[code delphi] function myMethod.moneyChinese(m:string):string; const MAXIMUM_NUMBER = 99999999999.99; CN_ZERO = '零' ; CN_ONE = '壹' ; CN_TWO = '貳' ; CN_THREE = '參'; CN_FOUR = '肆'; CN_FIVE = '伍'; CN_SIX = '陸' ; CN_SEVEN = '柒'; CN_EIGHT = '捌'; CN_NINE = '玖'; CN_TEN = '拾'; CN_HUNDRED = '佰'; CN_THOUSAND = '仟'; CN_TEN_THOUSAND = '萬'; CN_HUNDRED_MILLION = '億'; CN_SYMBOL = '新臺幣 '; CN_DOLLAR = '圓'; CN_TEN_CENT = '角'; CN_CENT = '分'; CN_INTEGER = '整'; digits: array[0..9] of string =(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE); radices:array[0..3] of string =('', CN_TEN, CN_HUNDRED, CN_THOUSAND); bigRadices :array[0..2] of string=('', CN_TEN_THOUSAND, CN_HUNDRED_MILLION); decimals :array[0..1] of string=(CN_TEN_CENT, CN_CENT); var integral:string; // Represent integral part of digit number. decimal:string; // Represent decimal part of digit number. outputCharacters:string; // The output result. parts:TStringList; zeroCount:Integer; i, p, d:Integer; quotient, modulus: Currency; moneyC:Currency; begin moneyC:=StrToCurrDef(m,0); if moneyC=0 then raise Exception.Create('必須是數字且大於零!!'); if moneyC>MAXIMUM_NUMBER then raise Exception.Create('超過最大數字....'); parts := TStringList.Create; parts.StrictDelimiter := true; // Delphi7 沒有這個屬性 :~( parts.Delimiter := '.'; // 指定逗點為分割字元 parts.DelimitedText := CurrToStr(moneyc); // 要被分割的字串 if (parts.Count > 1) then begin integral := parts[0]; decimal := parts[1]; // Cut down redundant decimal digits that are after the second. decimal := Copy(decimal,0, 2); end else begin integral := parts[0]; decimal := ''; end; outputCharacters:=''; if (StrToCurrDef(integral,0) > 0) then begin zeroCount := 0; i:=0; while i< Length(integral) do begin p := length(integral) - i -1 ; d := StrToInt(copy(integral,i 1, 1)); quotient := p/4; modulus := p mod 4; if (d=0) then begin zerocount:=zeroCount 1; end else begin if (zeroCount > 0) then begin outputCharacters:= outputCharacters digits[0]; end ; zeroCount := 0; outputCharacters:= outputCharacters digits[d] radices[StrToInt(currtostr(modulus))]; end; if (modulus = 0) and (zeroCount < 4) then begin outputCharacters :=outputCharacters bigRadices[StrToInt(currtostr(quotient))]; end; i:=i 1; end; outputCharacters :=outputCharacters CN_DOLLAR; end; if (decimal <> '') then begin for i := 0 to Length(decimal)-1 do begin d := StrToInt(Copy(decimal,i, 1)); if (d <> 0) then begin outputCharacters := outputCharacters digits[d] decimals[i]; end; end; end; if (decimal = '') then begin outputCharacters :=outputCharacters CN_INTEGER; end; outputCharacters := CN_SYMBOL outputCharacters; result:= outputCharacters; [/code] 從javascript 改過來的... |
GrandRURU
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
|
P.D.
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
感謝分享, 這是我寫的轉大寫金額公式, 提供參考
這是delphi5寫的 宣告兩組常數, 在 unit 下 [code delphi] unit MYFUNCTION; interface uses .... const CMoney: array[0..9] of string = ('零','壹','貳','參','肆','伍','陸','柒','捌','玖'); const CMoneyType: array [0..10] of string = ('','','拾','佰','仟','萬','拾','佰','仟','億','拾'); Function NTS(Value: string): string; var tmplength: integer; tmpfare: string; i: integer; xMoney: array [0..9] of integer; begin tmplength:= length(Value); tmpfare:= ''; for i:=0 to 9 do xMoney[i]:= 0; for i:=0 to tmplength-1 do begin xMoney[i]:= StrtoIntDef(copy(Value,i 1,1),9); if xMoney[i] <> 0 then tmpfare:= tmpfare CMoney[xMoney[i]] CMoneyType[tmplength-i]; // 如果超過萬元而剛好萬元為0, 如 7506254 // 上述功能會造成結果是 柒佰伍拾陸仟貳佰伍拾肆, 少掉萬字 if (xMoney[i]=0) and (tmplength-i=5) then tmpfare:= tmpfare CMoneyType[tmplength-i]; end; result:= trim(tmpfare); end; [/code] ps. 可能編輯器的問題, 我選擇 delphi code 貼上來, 仍然不見排版, 所以請忍耐著點看吧! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |