โปรแกรม แปลงตัวเลขเป็นตัวอักษร BathText
//****** Unit1.h //---------------------------------------------------------------------------
#ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TEdit *Number; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); AnsiString __fastcall TForm1::Getthai(AnsiString Value, int CountAll, int Position); AnsiString __fastcall TForm1::BahtText(double Money);
}; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
//***** Unit1.cpp //---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop
#include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) { ShowMessage(BahtText(StrToFloat(Number->Text))); } //---------------------------------------------------------------------------
AnsiString __fastcall TForm1::BahtText(double Money) { AnsiString TNumber[9] = {"","สอง","สาม","สี่","ห้า","หก","เจ็ด","แปด","เก้า"}; int i,CountReal,CountAll; AnsiString DecimalCh1,DecimalCh2,String2Process,Strthai,xReal,xDecimal, ThaiBaht,ThaiStang,curBahtStang,WordBaht,Result;
String2Process = FormatFloat("############0.00",Money); CountAll = String2Process.Trim().Length();
xReal = String2Process.SubString(1,CountAll-3); xDecimal = String2Process.SubString(CountAll-1,2); i = 0; CountReal = xReal.Length(); ThaiBaht = ""; ThaiStang = "";
//******************************************* // Main Working //******************************************* for(i=1; i<=CountReal; i++) // Loop = Real { curBahtStang = Getthai(xReal.SubString(i,1),CountReal,CountReal+1-i); if(curBahtStang=="x") { WordBaht = ""; curBahtStang = ""; } else WordBaht = "บาท";
ThaiBaht = ThaiBaht+curBahtStang; } ThaiBaht = ThaiBaht+WordBaht;
//******************************************* // Check Decimal I //******************************************* DecimalCh1 = xDecimal.SubString(1,1); if(DecimalCh1!="0") { if(DecimalCh1=="0" || DecimalCh1=="1" || DecimalCh1=="2") { if(DecimalCh1=="1") ThaiStang = ""; else if(DecimalCh1=="2") ThaiStang = "ยี่"; else ThaiStang = TNumber[StrToInt(DecimalCh1)];
ThaiStang = ThaiStang+"สิบ"; } }
//******************************************* // Check Decimal II //******************************************* DecimalCh2 = xDecimal.SubString(2,1); if(DecimalCh2=="0" || DecimalCh2=="1" || DecimalCh2=="2") { // ไม่สนใจ 0 if(DecimalCh2=="1") { if(xDecimal.SubString(1,1)=="0") ThaiStang = ThaiStang+"หนึ่ง"; else ThaiStang = ThaiStang+"เอ็ด"; } else if(DecimalCh2=="2") ThaiStang = ThaiStang+"สอง"; else ThaiStang = ThaiStang+TNumber[StrToInt(DecimalCh2)];
ThaiStang = ThaiStang+"สตางค์"; }
//******************************************* // Check Lakh //******************************************* if(xDecimal.SubString(1,1)!="0" || xDecimal.SubString(2,1)!="0") Strthai = ThaiBaht+ThaiStang; else if(ThaiBaht!="") Strthai = ThaiBaht+"ถ้วน"; else Strthai = "ศูนย์บาท";
Result = Strthai; return(Result); }
AnsiString __fastcall TForm1::Getthai(AnsiString Value, int CountAll, int Position) { AnsiString BahtStang, Result; AnsiString xUnit = ""; if(Value=="0") { if(Value=="0") { if(CountAll==1) BahtStang = "x"; // Flag ไม่ต้องเขียนคำว่า บาท } else BahtStang = ""; } else if(Value=="1") { switch(Position) { case 1: case 7: case 13: case 19: case 25: if(Position-CountAll!=0 || Value.SubString(Position+1,1)=="0") BahtStang = "เอ็ด"; else BahtStang = "หนึ่ง"; break;
case 2: case 8: case 14: case 20: case 26: BahtStang = ""; break;
default: BahtStang = "หนึ่ง"; break; } } else if(Value=="2") { if(Position==2 || Position==8 || Position==14) BahtStang = "ยี่"; else BahtStang ="สอง"; } else if(Value=="3") BahtStang = "สาม"; else if(Value=="4") BahtStang = "สี่"; else if(Value=="5") BahtStang = "ห้า"; else if(Value=="6") BahtStang = "หก"; else if(Value=="7") BahtStang = "เจ็ด"; else if(Value=="8") BahtStang = "แปด"; else if(Value=="9") BahtStang = "เก้า";
// Check Unit of Position if(Value!="0") { switch(Position) { case 7: case 13: case 19: case 25 : BahtStang = BahtStang+"ล้าน"; break; case 6: case 12: case 18: case 24 : BahtStang = BahtStang+"แสน"; break; case 5: case 11: case 17: case 23 : BahtStang = BahtStang+"หมื่น"; break; case 4: case 10: case 16: case 22 : BahtStang = BahtStang+"พัน"; break; case 3: case 9: case 15: case 21 : BahtStang = BahtStang+"ร้อย"; break; case 2: case 8: case 14: case 20 : BahtStang = BahtStang+"สิบ"; break; } } else if(Position==7 || Position==13 || Position==20) xUnit = "ล้าน";
Result = BahtStang+xUnit; return(Result); } //---------------------------------------------------------------------------
|