|
 |
- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -
|
|
 |
กระทู้ #3342 [Delphi] (จาก IP: 180.183.159.94)
โปรแกรมเปลียนตัวเลขเป็นภาษาไทย Delphi 7 [ขอความช่วยเหลือ]
ใครพอจะมีแนวทางไหมครับเช่น 120 = หนึ่งร้อยยี่สิบ
|
จากคุณ
:
Zen13 / i3enz_zen13@hotmail.com [2012-09-10 17:31:46]
|
|
ความคิดเห็น #28676 (จาก IP: 58.11.86.162)
ลองศึกษาแล้วเอามาแปลงเอง
------------------Cut---------------------------------- function ToWords (num : extended; wand, usd : boolean) : string; { this converts currency to words } { BOOLEAN PARAMETERS wand : if true use "and", if not don't usd : if true US Dollars, if not GB Pounds } const units : array[1..9] of string[6] = ('one ','two ','three ','four ','five ', 'six ','seven ','eight ','nine '); tens : array[2..9] of string[8] = ('twenty ','thirty ','forty ','fifty ', 'sixty ','seventy ','eighty ','ninety '); teens : array[0..9] of string[10] = ('ten ','eleven ','twelve ','thirteen ', 'fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '); sep = ['.',',']; var i : byte; bit : string[3]; base : string[15]; chunk, resstr : string; andflag : boolean; numstrings, lastcopy : integer; bits : tstringlist; curr, curr1, pennies : string[8]; penniesbit : string[12]; which : array[0..2] of string[9]; procedure dochunk(ch : string; pass : byte); var tflag : boolean; i : byte; pt : array[1..3] of integer; begin chunk:=''; tflag:=false; if ch='000' then if (resstr <> '') and (pass=2) then begin chunk:= curr; exit; end else exit; for i:=1 to 3 do pt[i]:=strtoint(ch[i]); if pt[1] <> 0 then chunk:=chunk+ units[pt[1]] + 'hundred '; if wand then begin if (copy(ch,2,2) <> '00') then if andflag or (chunk <> '') then chunk:=chunk + 'and '; end; if pt[2] <> 0 then begin if pt[2] > 1 then chunk:=chunk + tens[pt[2]] else tflag:=true; end; if tflag then begin chunk:= chunk + teens[pt[3]] end else if pt[3] <> 0 then chunk:=chunk + units[pt[3]]; if (pass = 2) and (resstr='') and (chunk='one ') then chunk:=chunk + curr1 else chunk:=chunk + which[pass]; andflag:=true; end; begin { ToWords } which[0]:='million '; which[1]:='thousand '; if usd then begin curr:='dollars '; curr1:='dollar '; pennies:=' cents'; end else begin curr:='pounds '; curr1:='pound '; pennies:=' pence'; end; which[2]:=curr; chunk:=''; resstr:=''; base:=formatfloat('000,000,000.00',num); try bits:=tstringlist.create; lastcopy:=1; for i := 1 to length(base) do if (base[i] in sep) or (i=length(base)) then begin bit:=copy(base,lastcopy,i-lastcopy); if i= length(base) then bit:=bit + base[i]; numstrings:=bits.add(bit); lastcopy:=i+1; end; andflag:=false; for i:= 0 to 2 do begin dochunk(bits[i],i); resstr:=resstr + chunk; chunk:=''; end; if usd then penniesbit:='and ' + bits[3] + ' cents' else penniesbit:=bits[3]; if bits[3] <> '00' then begin if resstr='' then resstr:=bits[3] + pennies else resstr:=resstr + penniesbit; end; if resstr <> '' then resstr[1]:=upcase(resstr[1]); result:=resstr; finally bits.free; end; end; { ToWords } ---------------------------Cut-------------------------- Hope it helps Nick Spurrier (MoDESoft, UK) |
จากคุณ
:
maddog [2012-09-13 10:42:49]
|
 |
ความคิดเห็น #28999 (จาก IP: 111.84.212.190)
ลองเอา code การหาค่าเงินบาท ที่ผมเขียนใน Lazarus ไปประยุกต์ใช้ดูครับ
------------Code----------------
function TThai.TextBaht(extBaht:Currency):string; var strBaht, strStang, strTmpBaht, strTmpStang : string; n, i : integer; isCredit : boolean; const kLuk : array [1..15] of string = ('','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน','สิบ','ร้อย','พัน','หมื่น','แสน','ล้านๆ','สิบ','ร้อย'); kNum : array [0..9] of string = ('','หนึ่ง','สอง','สาม','สี่','ห้า','หก','เจ็ด','แปด','เก้า'); begin Result := ''; isCredit := false; n := 0;
strBaht := FormatFloat('0.00',extBaht); // แปลงเลขเป็นสตริงก์ n := Pos('.',strBaht); // หาตำแหน่งจุด
if extBaht < 0 then begin //ถ้าค่าเป็นลบ // ค่าเงินเป็นลบ isCredit := true; strStang := Copy(strBaht,n+1,2); // หาเลขสตางค์ strBaht := Copy(strBaht,2,n-2); // หาเลขบาท end else begin // ค่าเงินเป็นบวก isCredit := false; strStang := Copy(strBaht,n+1,2); // หาเลขสตางค์ strBaht := Copy(strBaht,1,n-1); // หาเลขบาท end;
(* // คิดเฉพาะค่าเป็นบวกเท่านั้น strStang := Copy(strBaht,n+1,2); // หาเลขสตางค์ strBaht := Copy(strBaht,1,n-1); // หาเลขบาท *)
// หาบาท strTmpBaht := ''; i := Length(strBaht); for n := 1 to Length(strBaht) do begin // ถ้าเป็นเลข 0 if (strBaht[n]='0') then begin // ถ้าเป็นหลักล้าน และ มีเงินตั้งแต่ล้านขึ้นไปให้เติมคำว่า ล้าน if (i=7) and (strTmpBaht<>'') then strTmpBaht := strTmpBaht + kLuk[i]; Dec(i); // คือ i := i - 1; Continue; // ข้ามการทำงาน end; // บวกจำนวนเงินเข้าไปตามหลัก strTmpBaht := strTmpBaht + kNum[StrToInt(strBaht[n])] + kLuk[i]; Dec(i); end;
// ถ้ามียอดเงินให้เพิ่มคำว่า บาท if (strTmpBaht <> '') then strTmpBaht := strTmpBaht + 'บาท';
// หาสตางค์ strTmpStang := ''; // ถ้ามีค่าตั้งแต่ 10 สตางค์ขึ้นไป if (strStang[1]<>'0') then // สิบ strTmpStang := strTmpStang + kNum[StrToInt(strStang[1])] + kLuk[2]; // ถ้ามีค่าสตางค์ if (strStang[2]<>'0') then // หน่วย strTmpStang := strTmpStang + kNum[StrToInt(strStang[2])];
// ถ้ามีค่าหน่วยสตางค์ ให้เติมคำว่า สตางค์ if (strTmpStang<>'') then begin strTmpStang := strTmpStang + 'สตางค์'; strTmpBaht := strTmpBaht + strTmpStang; // รวมบาทสตางค์ end else if (strTmpBaht<>'') then // แต่ถ้ามีค่าเงินบาทให้เติม ถ้วน strTmpBaht := strTmpBaht + 'ถ้วน';
// โยกย้ายคำเรียกหน่วยนับ if (strTmpBaht<>'') then begin
strTmpBaht := StringReplace(strTmpBaht,'หนึ่งสิบ','สิบ',[rfReplaceAll]); strTmpBaht := StringReplace(strTmpBaht,'สองสิบ','ยี่สิบ',[rfReplaceAll]); strTmpBaht := StringReplace(strTmpBaht,'สิบหนึ่ง','สิบเอ็ด',[rfReplaceAll]);
// เห็นใช้กันได้ทั้งสองแบบ ร้อยหนึ่ง ร้อยเอ็ด //strTmpBaht := StringReplace(strTmpBaht,'ร้อยหนึ่ง','ร้อยเอ็ด',[rfReplaceAll]); if isCredit then Result := '(ติดลบ) ' + strTmpBaht else Result := strTmpBaht; end; end; ------------จบ---------------- |
จากคุณ
:
M-29 / m29@gmail.com [2014-01-31 19:21:41]
|
 |
|
- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -
|
|
|
|
|