[c#.net] สอบถามเรื่องการแปลงตัวเลขเป็นตัวอักษรครับ
ก้อมีอยู่นะเด๋วตอนเย็นมาโพสให้
Date :
2009-12-21 13:11:13
By :
tanapop
http://www.ninemee.com/?p=395
Date :
2009-12-21 14:24:45
By :
mee079
ขอบคุณครับ
Date :
2009-12-21 17:31:01
By :
yordafone
Go to : Convert number to Thai bath with Excel by .Net
Code (VB.NET)
Function changenum(num As String) As String
Dim i As Integer, max As Integer, r As String, n As String
num = Trim(Str(Val(num)))
max = Len(num)
For i = 1 To max
r = Choose(((max - i + 1) Mod 6) + 1, "แสน", "", "สิบ", "ร้อย", "พัน", "หมื่น")
n = Choose(Mid(num, i, 1) + 1, "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า")
If r = "สิบ" And n = "หนึ่ง" Then n = ""
If n = "หนึ่ง" And r = "" And max <> 1 Then n = "เอ็ด"
If i = 1 And n = "เอ็ด" And max > 1 Then n = "หนึ่ง"
If r = "สิบ" And n = "สอง" Then n = "ยี่"
If r = "" And max - i + 1 > 6 Then r = "ล้าน"
If n <> "ศูนย์" Then
changenum = changenum & n & r
Else
If r = "ล้าน" Then changenum = changenum & r
End If
Next
End Function
Go to : vb พอจะแปลงตัวเลข เป็น อักษรได้หรือเปล่าคับ ต้องใช้ฟังก์ชันอะไรบ้าง
Go to : อยากทราบวิธีแปลงตัวเลขเป็นตัวหนังสือครับ VB รบกวนด้วยครับขอแบบมีสตางค์ด้วยนะครับ ขอบคุณครับ
Date :
2011-11-04 11:41:27
By :
webmaster
Code (C#)
public static string ToWord(decimal PriceAmount)
{
try
{
return MoneyToWordBath(PriceAmount) + MoneyToWordStarg(PriceAmount);
}
catch
{
return string.Empty;
}
}
public static string MoneyToWordBath(decimal PriceAmount)
{
try
{
string Price = Math.Truncate(PriceAmount).ToString();
int len = Price.Length;
if (len > 13)
{
MessageBox.Show("ไม่รองรับ !");
return "";
}
string Word = string.Empty;
string m = string.Empty;
string n = string.Empty;
for (int i = 0; i < Price.Length; i++)
{
n = string.Empty;
m = string.Empty;
n = (ltxt().FindAll(p => p.Num == Price.ToArray()[i].ToString()).FirstOrDefault().Word);
if (Price.ToArray()[i].ToString() != "0")
{
m = (lMoney().FindAll(p => p.index == len.ToString()).FirstOrDefault().word ?? string.Empty);
}
//if (item.ToString() == "1" && len == 1 && Price.Length >= 2) { n = "เอ็ด"; }
if (Price.ToArray()[i].ToString() == "1" && len == 1 && Price.Length >= 2)
{
var index = Price.ToList()[i - 1];
n = "เอ็ด";
if (index.ToString() == "0")
{
n = "หนึ่ง";
}
}
if (Price.ToArray()[i].ToString() == "1" && len == 7 && Price.Length >= 8)
{
var index = Price.ToList()[i-1];
n = "เอ็ด";
if (index.ToString() == "0")
{
n = "หนึ่ง";
}
}
if (Price.ToArray()[i].ToString() == "1" && (len == 2 || len == 8)) { n = ""; }
if (Price.ToArray()[i].ToString() == "2" && (len == 2 || len == 8)) { n = "ยี่"; }
Word += (n + m); len--;
}
return Word == string.Empty ? "" : Word + "บาท";
}
catch (Exception)
{
return string.Empty;
}
}
public static string MoneyToWordStarg(decimal PriceAmount)
{
try
{
decimal dc = (PriceAmount - Math.Truncate(PriceAmount));
if (!dc.ToString("n2").Contains('.')) { return string.Empty; }
string Price = dc.ToString("n2").Split('.')[1];
int len = Price.Length;
if (len > 13)
{
MessageBox.Show("ไม่รองรับ !");
return string.Empty;
}
string Word = string.Empty;
string m = string.Empty;
string n = string.Empty;
foreach (var item in Price.ToArray())
{
n = string.Empty;
m = string.Empty;
n = (ltxt().FindAll(p => p.Num == item.ToString()).FirstOrDefault().Word);
if (item.ToString() != "0")
{
m = (lMoney().FindAll(p => p.index == len.ToString()).FirstOrDefault().word ?? string.Empty);
}
if (item.ToString() == "1" && len == 1 && Price.Length >= 2) { n = "เอ็ด"; }
if (item.ToString() == "1" && (len == 2 || len == 8)) { n = ""; }
if (item.ToString() == "2" && (len == 2 || len == 8)) { n = "ยี่"; }
Word += (n + m);
len--;
}
return Word == string.Empty ? "ถ้วน" : Word + "สตางค์";
}
catch
{
return string.Empty;
}
}
static List<NumberList> ltxt()
{
var txt = new List<NumberList>();
txt.Add(new NumberList { Num = "1", Word = "หนึ่ง" });
txt.Add(new NumberList { Num = "2", Word = "สอง" });
txt.Add(new NumberList { Num = "3", Word = "สาม" });
txt.Add(new NumberList { Num = "4", Word = "สี่" });
txt.Add(new NumberList { Num = "5", Word = "ห้า" });
txt.Add(new NumberList { Num = "6", Word = "หก" });
txt.Add(new NumberList { Num = "7", Word = "เจ็ด" });
txt.Add(new NumberList { Num = "8", Word = "แปด" });
txt.Add(new NumberList { Num = "9", Word = "เก้า" });
txt.Add(new NumberList { Num = "0", Word = "" });
return txt;
}
static List<UnitList> lMoney()
{
var wmney = new List<UnitList>();
wmney.Add(new UnitList { index = "1", word = "" });
wmney.Add(new UnitList { index = "2", word = "สิบ" });
wmney.Add(new UnitList { index = "3", word = "ร้อย" });
wmney.Add(new UnitList { index = "4", word = "พัน" });
wmney.Add(new UnitList { index = "5", word = "หมื่น" });
wmney.Add(new UnitList { index = "6", word = "แสน" });
wmney.Add(new UnitList { index = "7", word = "ล้าน" });
wmney.Add(new UnitList { index = "8", word = "สิบ" });
wmney.Add(new UnitList { index = "9", word = "ร้อย" });
wmney.Add(new UnitList { index = "10", word = "พัน" });
wmney.Add(new UnitList { index = "11", word = "หมื่น" });
wmney.Add(new UnitList { index = "12", word = "แสน" });
wmney.Add(new UnitList { index = "13", word = "ล้านล้าน" });
return wmney;
}
}
public class NumberList
{
public string Num { get; set; }
public string Word { get; set; }
}
public class UnitList
{
public string index { get; set; }
public string word { get; set; }
}
Date :
2019-11-05 10:57:34
By :
anonymous
Load balance : Server 02