|
|
|
ใครมี Funtoin BathThai มั่งครับ VB.Net นะครับ ผมหาโค้ดเก่าที่เขียนไว้ไม่เจอ |
|
|
|
|
|
|
|
Code (VB.NET)
Function BahtText(ByVal sNum)
Dim sNumber As String() = {"", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า"}
Dim sDigit As String() = {"", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน"}
Dim sDigit10 As String() = {"", "สิบ", "ยี่สิบ", "สามสิบ", "สี่สิบ", "ห้าสิบ", "หกสิบ", "เจ็ดสิบ", "แปดสิบ", "เก้าสิบ"}
Dim nLen, sWord, sWord2
Dim sByte1, I, J
sNum = Replace(FormatNumber(sNum, 2), ",", "")
nLen = Len(sNum)
If sNum = ".00" Then BahtText = "ศูนย์"
For I = 1 To nLen - 3
J = (15 + nLen - I) Mod 6
sByte1 = Mid(sNum, I, 1)
If sByte1 <> "0" Then
If J = 1 Then sWord = sDigit10(sByte1) Else sWord = sNumber(sByte1) & sDigit(J)
BahtText = BahtText & sWord
End If
If J = 0 And I <> nLen - 3 Then BahtText = BahtText & "ล้าน" : BahtText = Replace(BahtText, "หนึ่งล้าน", "เอ็ดล้าน")
Next
If Microsoft.VisualBasic.Left(sNum, 1) = "1" Then BahtText = Replace(BahtText, "เอ็ดล้าน", "หนึ่งล้าน")
If Microsoft.VisualBasic.Left(sNum, 2) = "11" Then BahtText = Replace(BahtText, "สิบหนึ่งล้าน", "สิบเอ็ดล้าน")
If Len(BahtText) > 0 Then BahtText = BahtText & "บาท"
If nLen > 4 Then BahtText = Replace(BahtText, "หนึ่งบาท", "เอ็ดบาท")
sNum = Microsoft.VisualBasic.Right(sNum, 2)
If sNum = "00" Then
BahtText = BahtText & "ถ้วน"
Else
If Microsoft.VisualBasic.Left(sNum, 1) <> "0" Then BahtText = BahtText & sDigit10(Microsoft.VisualBasic.Left(sNum, 1))
If Microsoft.VisualBasic.Right(sNum, 1) <> "0" Then BahtText = BahtText & sNumber(Microsoft.VisualBasic.Right(sNum, 1))
BahtText = BahtText & "สตางค์"
If Microsoft.VisualBasic.Left(sNum, 1) <> "0" Then BahtText = Replace(BahtText, "หนึ่งสตางค์", "เอ็ดสตางค์")
End If
End Function
|
|
|
|
|
Date :
2014-12-06 10:24:30 |
By :
zarooman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
double number = 0.0;
if (textBox1.Text.Length > 0)
{
try
{
number = Convert.ToDouble(textBox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
label1.Text = "To Thai: " + number.ToThai("#,##0.####");
label2.Text = "To Text: " + number.ToText();
label3.Text = "To Baht: " + number.ToBaht();
}
}
ExtensionMethods.cs
using System;
using System.Collections.Generic;
using System.Linq;
namespace WindowsFormsApplication1
{
public static class ExtensionMethods
{
public static string ToThai(this double number)
{
return ThaiNumber(number.ToString());
}
public static string ToThai(this double number, string format)
{
return ThaiNumber(number.ToString(format));
}
public static string ToThai(this double number, IFormatProvider format)
{
return ThaiNumber(number.ToString(format));
}
public static string ToText(this double number)
{
string[] text = number.ToString().Split('.');
string inttext = IntToText(text[0]);
string dectext = (text.Length > 1) ? DecToText(text[1]) : string.Empty;
return (text.Length == 1) ? ((inttext != string.Empty) ? inttext : "ศูนย์") : string.Format("{0}จุด{1}", (inttext != string.Empty) ? inttext : "ศูนย์", dectext);
}
public static string ToBaht(this double number)
{
string[] text = number.ToString().Split('.');
string inttext = IntToText(text[0]);
string dectext = (text.Length > 1) ? (text[1].Length <= 2) ? ((inttext != string.Empty) ? "บาท" : string.Empty) + IntToText((text[1].Length == 1) ? text[1] + "0" : text[1]) + "สตางค์" : ((inttext != string.Empty) ? string.Empty : "ศูนย์") + "จุด" + DecToText(text[1]) + "บาท" : string.Empty;
return (text.Length == 1) ? (inttext == string.Empty) ? "ศูนย์บาท" : inttext + "บาทถ้วน" : inttext + dectext;
}
private static string ThaiNumber(string text)
{
string result = string.Empty;
char[] thai = new char[] { '๐', '๑', '๒', '๓', '๔', '๕', '๖', '๗', '๘', '๙' };
foreach (char c in text.ToCharArray())
{
result += (!char.IsDigit(c)) ? c : thai[Convert.ToInt16(c.ToString())];
}
return result;
}
private static string IntToText(string str)
{
List<string> text = new List<string>();
List<string> num = SplitString(str, 6);
for (int i = 0; i < num.Count; i++)
{
string numThai = NumToThai(num[i]) + string.Join(string.Empty, Enumerable.Repeat<string>("ล้าน", i).ToArray());
text.Add(numThai);
}
string[] result = text.ToArray();
Array.Reverse(result);
return string.Join(string.Empty, result);
}
private static string DecToText(string str)
{
string[] thai = new string[] { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า" };
string result = string.Empty;
foreach (char c in str.ToCharArray())
{
result += thai[Convert.ToInt16(c.ToString())];
}
return result;
}
private static string NumToThai(string str)
{
string[] position = new string[] { string.Empty, "สิบ", "ร้อย", "พัน", "หมื่น", "แสน" };
string[] thai = new string[] { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า" };
char[] charArray = str.ToCharArray();
List<string> text = new List<string>();
for (int i = 0; i < str.Length; i++)
{
if (charArray[i] != '0')
{
if (charArray[i] == '1' && i == 0)
{
text.Add((str.Length > 1) ? (Convert.ToInt64(str) != 10) ? "เอ็ด" : "หนึ่ง" : "หนึ่ง");
}
else if (charArray[i] == '1' && i == 1)
{
text.Add(position[i]);
}
else if (charArray[i] == '2' && i == 1)
{
text.Add("ยี่" + position[i]);
}
else
{
text.Add(thai[Convert.ToInt16(charArray[i].ToString())] + position[i]);
}
}
}
string[] result = text.ToArray();
Array.Reverse(result);
return string.Join(string.Empty, result);
}
private static List<string> SplitString(string str, int length)
{
List<string> result = new List<string>();
string text = string.Empty;
char[] charArray = str.ToCharArray();
for (int i = str.Length - 1; i >= 0 ; i--)
{
if (text.Length < length)
{
text += charArray[i];
}
if (i == 0 || text.Length == length)
{
result.Add(text);
text = string.Empty;
}
}
return result;
}
}
}
|
|
|
|
|
Date :
2014-12-08 13:57:09 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|