public ThaiBahtTextOptions(UsesEt usesEt       = UsesEt.TensOnly,
                            Unit unit           = Unit.Baht,
                            int decimalPlaces   = 2,
                            bool appendBahtOnly = true)
 {
     this.UsesEt         = usesEt;
     this.Unit           = unit;
     this.AppendBahtOnly = appendBahtOnly;
 }
 public ThaiBahtTextOptions(UsesEt usesEt = UsesEt.TensOnly,
                        Unit unit = Unit.Baht,
                        int decimalPlaces = 2,
                        bool appendBahtOnly = true)
 {
     this.UsesEt = usesEt;
       this.Unit = unit;
       this.AppendBahtOnly = appendBahtOnly;
 }
 public string ThaiBahtText(
     decimal amount,
     UsesEt usesEt,
     Unit unit,
     int decimalPlaces,
     bool appendBahtOnly
     )
 {
     string result = ThaiBahtTextUtil.ThaiBahtText(amount, usesEt, unit, decimalPlaces, appendBahtOnly);
       return result;
       // TODO: add assertions to method ThaiBahtTextUtilTest.ThaiBahtText(Decimal, UsesEt, Unit, Int32, Boolean)
 }
示例#4
0
        /// <summary>
        /// ให้ข้อความจำนวนเงินภาษาไทย เช่น จำนวน 121.50 บาท จะให้ผลลัพธ์เป็น "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"
        /// </summary>
        /// <param name="amount">จำนวนเงิน</param>
        /// <returns>ข้อความจำนวนเงินภาษาไทย</returns>
        public static string ThaiBahtText(this decimal amount, UsesEt mode = UsesEt.TensOnly)
        {
            if (amount == 0)
            {
                return("ศูนย์บาทถ้วน");
            }

            var result = new StringBuilder();

            amount = Math.Round(amount, 2, MidpointRounding.AwayFromZero);

            if (amount < MinValue || MaxValue < amount)
            {
                throw new NotSupportedException();
            }

            if (amount < 0)
            {
                result.Append("ลบ");
                amount = -amount;
            }

            string[] parts = decompose(amount);

            if (parts[0].Length > 0)
            {
                result.Append(speak(parts[0], mode));
                result.Append("ล้าน");
            }

            if (parts[1].Length > 0)
            {
                result.Append(speak(parts[1], mode));
                result.Append("บาท");
            }
            else if (parts[0].Length > 0)
            {
                result.Append("บาท");
            }

            if (parts[2].Length > 0)
            {
                result.Append(speak(parts[2], mode));
                result.Append("สตางค์");
            }
            else
            {
                result.Append("ถ้วน");
            }

            return(result.ToString());
        }
        public string ThaiBahtText(
            decimal amount,
            UsesEt usesEt,
            Unit unit,
            int decimalPlaces,
            bool appendBahtOnly
            )
        {
            string result = ThaiBahtTextUtil.ThaiBahtText(amount, usesEt, unit, decimalPlaces, appendBahtOnly);

            return(result);
            // TODO: add assertions to method ThaiBahtTextUtilTest.ThaiBahtText(Decimal, UsesEt, Unit, Int32, Boolean)
        }
        /// <summary>
        /// ให้ข้อความจำนวนเงินภาษาไทย เช่น จำนวน 121.50 บาท จะให้ผลลัพธ์เป็น "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"
        /// </summary>
        /// <param name="amount">จำนวนเงิน</param>
        /// <param name="usesEt">รูปแบบการใช้เอ็ดสำหรับค่าหนึ่งที่หลักหน่วย</param>
        /// <param name="unit">หน่วยของจำนวนเงิน</param>
        /// <param name="appendBahtOnly">เพิ่มคำว่า 'ถ้วน' ท้ายข้อความ</param>
        /// <returns>ข้อความจำนวนเงินภาษาไทย</returns>
        public static string ThaiBahtText(this decimal?amount,
                                          UsesEt usesEt       = UsesEt.Always,
                                          Unit unit           = Unit.Baht,
                                          int decimalPlaces   = 2,
                                          bool appendBahtOnly = true)
        {
            Contract.Ensures(Contract.Result <string>() != null);
            Contract.Ensures(Contract.Result <string>().Length > 0);

            return(ThaiBahtText(amount ?? 0m,
                                usesEt: usesEt,
                                unit: unit,
                                decimalPlaces: decimalPlaces,
                                appendBahtOnly: appendBahtOnly));
        }
        /// <summary>
        /// ให้ข้อความจำนวนเงินภาษาไทย เช่น จำนวน 121.50 บาท จะให้ผลลัพธ์เป็น "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"
        /// </summary>
        /// <param name="amount">จำนวนเงิน</param>
        /// <param name="usesEt">รูปแบบการใช้เอ็ดสำหรับค่าหนึ่งที่หลักหน่วย</param>
        /// <param name="unit">หน่วยของจำนวนเงิน</param>
        /// <param name="appendBahtOnly">เพิ่มคำว่า 'ถ้วน' ท้ายข้อความ</param>
        /// <returns>ข้อความจำนวนเงินภาษาไทย</returns>
        public static string ThaiBahtText(this decimal? amount,
                                      UsesEt usesEt = UsesEt.Always,
                                      Unit unit = Unit.Baht,
                                      int decimalPlaces = 2,
                                      bool appendBahtOnly = true)
        {
            Contract.Ensures(Contract.Result<string>() != null);
              Contract.Ensures(Contract.Result<string>().Length > 0);

              return ThaiBahtText(amount ?? 0m,
                          usesEt: usesEt,
                          unit: unit,
                          decimalPlaces: decimalPlaces,
                          appendBahtOnly: appendBahtOnly);
        }
        private static void speakTo(StringBuilder sb, string text, UsesEt mode)
        {
            Contract.Requires(text != null);
              Contract.Requires(text.Length > 0);

              int length = text.Length;
              int c = 0;
              int lastc = -1;
              bool negative = false;

              for (int i = 0; i < length; i++) {
            if (text[i] == '-') {
              negative = true;
            }
            else {
              c = int.Parse(text[i].ToString());

              if ((i == length - 1) && (c == 1)) {
            if (length == 1                  //  1
              || (negative && length == 2)   // -1
              || (length == 2 && lastc == 0) // 01 (satang)
              ) {
              sb.Append("หนึ่ง");
              return;
            }
            if (mode == UsesEt.Always) {
              sb.Append("เอ็ด");
            }
            else { // if (mode == UsesEt.TensOnly) {
              if (lastc == 0) {
                sb.Append("หนึ่ง");
              }
              else {
                sb.Append("เอ็ด");
              }
            }
              }
              else if ((i == length - 2) && (c == 2)) {
            sb.Append("ยี่สิบ");
              }
              else if ((i == length - 2) && (c == 1)) {
            sb.Append("สิบ");
              }
              else if (c != 0) {
            sb.Append(thaiNumbers[c] + thaiPlaces[length - i]);
              }
            }
            lastc = c;
              }
        }
        /// <summary>
        /// ให้ข้อความจำนวนเงินภาษาไทย เช่น จำนวน 121.50 บาท จะให้ผลลัพธ์เป็น "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"
        /// </summary>
        /// <param name="amount">จำนวนเงิน</param>
        /// <param name="usesEt">รูปแบบการใช้เอ็ดสำหรับค่าหนึ่งที่หลักหน่วย</param>
        /// <param name="unit">หน่วยของจำนวนเงิน</param>
        /// <param name="appendBahtOnly">เพิ่มคำว่า 'ถ้วน' ท้ายข้อความ</param>
        /// <returns>ข้อความจำนวนเงินภาษาไทย</returns>
        public static string ThaiBahtText(this decimal amount,
                                      UsesEt usesEt = UsesEt.Always,
                                      Unit unit = Unit.Baht,
                                      int decimalPlaces = 2,
                                      bool appendBahtOnly = true)
        {
            Contract.Ensures(Contract.Result<string>() != null);
              Contract.Ensures(Contract.Result<string>().Length > 0);

              var result = new StringBuilder();

              if (amount == 0) {
            switch (unit) {
              case Unit.Baht: result.Append("ศูนย์บาท"); break;
              case Unit.Million: result.Append("ศูนย์ล้านบาท"); break;
              case Unit.Billion: result.Append("ศูนย์พันล้านบาท"); break;
              case Unit.Trillion: result.Append("ศูนย์ล้านล้านบาท"); break;
            }
            if (appendBahtOnly) {
              result.Append("ถ้วน");
            }

            return result.ToString();
              }

              string format = "#.00";
              bool isBaht = unit == Unit.Baht;

              if (!isBaht) {
            switch (unit) {
              case Unit.Million: amount /= 1000000.0m; break;
              case Unit.Billion: amount /= 1000000000.0m; break;
              case Unit.Trillion: amount /= 1000000000000.0m; break;
            }
            switch (decimalPlaces) {
              case 0: format = "0.0"; break; // we still need satang
              case 1: format = "0.0"; break;
              case 2: format = "0.0#"; break;
              case 3: format = "0.0##"; break;
              case 4: format = "0.0###"; break;
              case 5: format = "0.0####"; break;
              case 6: format = "0.0#####"; break;
              default:
            format = "0.0#";
            decimalPlaces = 2;
            break;
            }
              }
              else {
            decimalPlaces = 2; // always 2 for unit Baht
              }

              amount = Math.Round(amount, decimalPlaces, MidpointRounding.AwayFromZero);

              if (amount < MinValue || MaxValue < amount) {
            throw new NotSupportedException();
              }

              if (amount < 0) {
            result.Append("ลบ");
            amount = -amount;
              }

              string text = amount.ToString(format);
              string[] parts = decompose(text);

              if (parts[0].Length > 0) {
            speakTo(result, parts[0], usesEt);
            result.Append("ล้าน");
              }

              if (parts[1].Length > 0) {
            speakTo(result, parts[1], usesEt);
            result.Append("ล้าน");
              }

              if (parts[2].Length > 0) {
            speakTo(result, parts[2], usesEt);
            if (isBaht) result.Append("บาท");
              }
              else if (parts[1].Length > 0) {
            if (isBaht) result.Append("บาท");
              }

              if (parts[3].Length > 0) {
            if (isBaht) {
              speakTo(result, parts[3], usesEt);
              result.Append("สตางค์");
            }
            else {
              if (int.Parse(parts[3]) != 0) {
            speakDotTo(result, parts[3]);
              }
              switch (unit) {
            case Unit.Million: result.Append("ล้านบาท"); break;
            case Unit.Billion: result.Append("พันล้านบาท"); break;
            case Unit.Trillion: result.Append("ล้านล้านบาท"); break;
              }
            }
              }
              else {
            if (appendBahtOnly) {
              result.Append("ถ้วน");
            }
              }

              return result.ToString();
        }
示例#10
0
        private static void speakTo(StringBuilder sb, string text, UsesEt mode)
        {
            Contract.Requires(text != null);
            Contract.Requires(text.Length > 0);

            int  length   = text.Length;
            int  c        = 0;
            int  lastc    = -1;
            bool negative = false;

            for (int i = 0; i < length; i++)
            {
                if (text[i] == '-')
                {
                    negative = true;
                }
                else
                {
                    c = int.Parse(text[i].ToString());

                    if ((i == length - 1) && (c == 1))
                    {
                        if (length == 1 ||               //  1
                            (negative && length == 2) || // -1
                            (length == 2 && lastc == 0)  // 01 (satang)
                            )
                        {
                            sb.Append("หนึ่ง");
                            return;
                        }
                        if (mode == UsesEt.Always)
                        {
                            sb.Append("เอ็ด");
                        }
                        else // if (mode == UsesEt.TensOnly) {
                        {
                            if (lastc == 0)
                            {
                                sb.Append("หนึ่ง");
                            }
                            else
                            {
                                sb.Append("เอ็ด");
                            }
                        }
                    }
                    else if ((i == length - 2) && (c == 2))
                    {
                        sb.Append("ยี่สิบ");
                    }
                    else if ((i == length - 2) && (c == 1))
                    {
                        sb.Append("สิบ");
                    }
                    else if (c != 0)
                    {
                        sb.Append(thaiNumbers[c] + thaiPlaces[length - i]);
                    }
                }
                lastc = c;
            }
        }
示例#11
0
        /// <summary>
        /// ให้ข้อความจำนวนเงินภาษาไทย เช่น จำนวน 121.50 บาท จะให้ผลลัพธ์เป็น "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"
        /// </summary>
        /// <param name="amount">จำนวนเงิน</param>
        /// <param name="usesEt">รูปแบบการใช้เอ็ดสำหรับค่าหนึ่งที่หลักหน่วย</param>
        /// <param name="unit">หน่วยของจำนวนเงิน</param>
        /// <param name="appendBahtOnly">เพิ่มคำว่า 'ถ้วน' ท้ายข้อความ</param>
        /// <returns>ข้อความจำนวนเงินภาษาไทย</returns>
        public static string ThaiBahtText(this decimal amount,
                                          UsesEt usesEt       = UsesEt.Always,
                                          Unit unit           = Unit.Baht,
                                          int decimalPlaces   = 2,
                                          bool appendBahtOnly = true)
        {
            Contract.Ensures(Contract.Result <string>() != null);
            Contract.Ensures(Contract.Result <string>().Length > 0);

            var result = new StringBuilder();

            if (amount == 0)
            {
                switch (unit)
                {
                case Unit.Baht: result.Append("ศูนย์บาท"); break;

                case Unit.Million: result.Append("ศูนย์ล้านบาท"); break;

                case Unit.Billion: result.Append("ศูนย์พันล้านบาท"); break;

                case Unit.Trillion: result.Append("ศูนย์ล้านล้านบาท"); break;
                }
                if (appendBahtOnly)
                {
                    result.Append("ถ้วน");
                }

                return(result.ToString());
            }

            string format = "#.00";
            bool   isBaht = unit == Unit.Baht;

            if (!isBaht)
            {
                switch (unit)
                {
                case Unit.Million: amount /= 1000000.0m; break;

                case Unit.Billion: amount /= 1000000000.0m; break;

                case Unit.Trillion: amount /= 1000000000000.0m; break;
                }
                switch (decimalPlaces)
                {
                case 0: format = "0.0"; break; // we still need satang

                case 1: format = "0.0"; break;

                case 2: format = "0.0#"; break;

                case 3: format = "0.0##"; break;

                case 4: format = "0.0###"; break;

                case 5: format = "0.0####"; break;

                case 6: format = "0.0#####"; break;

                default:
                    format        = "0.0#";
                    decimalPlaces = 2;
                    break;
                }
            }
            else
            {
                decimalPlaces = 2; // always 2 for unit Baht
            }

            amount = Math.Round(amount, decimalPlaces, MidpointRounding.AwayFromZero);

            if (amount < MinValue || MaxValue < amount)
            {
                throw new NotSupportedException();
            }

            if (amount < 0)
            {
                result.Append("ลบ");
                amount = -amount;
            }

            string text = amount.ToString(format);

            string[] parts = decompose(text);

            if (parts[0].Length > 0)
            {
                speakTo(result, parts[0], usesEt);
                result.Append("ล้าน");
            }

            if (parts[1].Length > 0)
            {
                speakTo(result, parts[1], usesEt);
                result.Append("ล้าน");
            }

            if (parts[2].Length > 0)
            {
                speakTo(result, parts[2], usesEt);
                if (isBaht)
                {
                    result.Append("บาท");
                }
            }
            else if (parts[1].Length > 0)
            {
                if (isBaht)
                {
                    result.Append("บาท");
                }
            }

            if (parts[3].Length > 0)
            {
                if (isBaht)
                {
                    speakTo(result, parts[3], usesEt);
                    result.Append("สตางค์");
                }
                else
                {
                    if (int.Parse(parts[3]) != 0)
                    {
                        speakDotTo(result, parts[3]);
                    }
                    switch (unit)
                    {
                    case Unit.Million: result.Append("ล้านบาท"); break;

                    case Unit.Billion: result.Append("พันล้านบาท"); break;

                    case Unit.Trillion: result.Append("ล้านล้านบาท"); break;
                    }
                }
            }
            else
            {
                if (appendBahtOnly)
                {
                    result.Append("ถ้วน");
                }
            }

            return(result.ToString());
        }
示例#12
0
 /// <summary>
 /// ให้ข้อความจำนวนเงินภาษาไทย เช่น จำนวน 121.50 บาท จะให้ผลลัพธ์เป็น "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"
 /// </summary>
 /// <param name="amount">จำนวนเงิน</param>
 /// <returns>ข้อความจำนวนเงินภาษาไทย</returns>
 public static string ThaiBahtText(this decimal?amount, UsesEt mode = UsesEt.TensOnly)
 {
     return(ThaiBahtText(amount.HasValue ? amount.Value : 0m));
 }
示例#13
0
        private static string speak(string text, UsesEt mode)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(string.Empty);
            }

            int    length   = text.Length;
            string result   = string.Empty;
            int    c        = 0;
            int    lastc    = -1;
            bool   negative = false;

            for (int i = 0; i < length; i++)
            {
                if (text[i] == '-')
                {
                    negative = true;
                }
                else
                {
                    c = int.Parse(text[i].ToString());

                    if ((i == length - 1) && (c == 1))
                    {
                        if (length == 1 ||               //  1
                            (negative && length == 2) || // -1
                            (length == 2 && lastc == 0)  // 01 (satang)
                            )
                        {
                            result += "หนึ่ง";
                            return(result);
                        }
                        if (mode == UsesEt.Always)
                        {
                            result += "เอ็ด";
                        }
                        else if (mode == UsesEt.TensOnly)
                        {
                            if (lastc == 0)
                            {
                                result += "หนึ่ง";
                            }
                            else
                            {
                                result += "เอ็ด";
                            }
                        }
                    }
                    else if ((i == length - 2) && (c == 2))
                    {
                        result += "ยี่สิบ";
                    }
                    else if ((i == length - 2) && (c == 1))
                    {
                        result += "สิบ";
                    }
                    else if (c != 0)
                    {
                        result += thaiNumbers[c] + thaiPlaces[length - i];
                    }
                }
                lastc = c;
            }

            return(result);
        }