-
Notifications
You must be signed in to change notification settings - Fork 966
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1032 from czbas23/master
Support Thai formatter and number to word
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Humanizer.Tests/Localisation/th-TH/DateHumanizeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Globalization; | ||
using Humanizer.Localisation; | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Localisation.thTH | ||
{ | ||
[UseCulture("th-TH")] | ||
public class DateHumanizeTests | ||
{ | ||
[Theory] | ||
[InlineData(1, "หนึ่งวินาทีที่แล้ว")] | ||
[InlineData(10, "10 วินาทีที่แล้ว")] | ||
[InlineData(59, "59 วินาทีที่แล้ว")] | ||
[InlineData(60, "หนึ่งนาทีที่แล้ว")] | ||
public void SecondsAgo(int seconds, string expected) | ||
{ | ||
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Past); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Humanizer.Tests/Localisation/th-TH/NumberToWordsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Globalization; | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Localisation.thTH | ||
{ | ||
[UseCulture("th-TH")] | ||
public class NumberToWordsTests | ||
{ | ||
[InlineData(1, "หนึ่ง")] | ||
[InlineData(10, "สิบ")] | ||
[InlineData(11, "สิบเอ็ด")] | ||
[InlineData(20, "ยี่สิบ")] | ||
[InlineData(-122, "ลบหนึ่งร้อยยี่สิบสอง")] | ||
[InlineData(3501, "สามพันห้าร้อยหนึ่ง")] | ||
[InlineData(100, "หนึ่งร้อย")] | ||
[InlineData(1000, "หนึ่งพัน")] | ||
[InlineData(10000, "หนึ่งหมื่น")] | ||
[InlineData(-100000, "ลบหนึ่งแสน")] | ||
[InlineData(1000000, "หนึ่งล้าน")] | ||
[InlineData(10000000, "สิบล้าน")] | ||
[InlineData(100000000, "หนึ่งร้อยล้าน")] | ||
[InlineData(1000000000, "หนึ่งพันล้าน")] | ||
[InlineData(111, "หนึ่งร้อยสิบเอ็ด")] | ||
[InlineData(1111, "หนึ่งพันหนึ่งร้อยสิบเอ็ด")] | ||
[InlineData(-111111, "ลบหนึ่งแสนหนึ่งหมื่นหนึ่งพันหนึ่งร้อยสิบเอ็ด")] | ||
[InlineData(1111111, "หนึ่งล้านหนึ่งแสนหนึ่งหมื่นหนึ่งพันหนึ่งร้อยสิบเอ็ด")] | ||
[InlineData(11111111, "สิบเอ็ดล้านหนึ่งแสนหนึ่งหมื่นหนึ่งพันหนึ่งร้อยสิบเอ็ด")] | ||
[InlineData(111111111, "หนึ่งร้อยสิบเอ็ดล้านหนึ่งแสนหนึ่งหมื่นหนึ่งพันหนึ่งร้อยสิบเอ็ด")] | ||
[InlineData(1111111111, "หนึ่งพันหนึ่งร้อยสิบเอ็ดล้านหนึ่งแสนหนึ่งหมื่นหนึ่งพันหนึ่งร้อยสิบเอ็ด")] | ||
[Theory] | ||
public void ToWords(int number, string expected) | ||
{ | ||
Assert.Equal(expected, number.ToWords()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/Humanizer/Localisation/NumberToWords/ThaiNumberToWordsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Humanizer.Localisation.NumberToWords | ||
{ | ||
internal class ThaiNumberToWordsConverter : GenderlessNumberToWordsConverter | ||
{ | ||
public override string Convert(long numbermoney) | ||
{ | ||
var Textreturn = ""; | ||
if (numbermoney == 0) | ||
{ | ||
return "ศูนย์"; | ||
} | ||
|
||
if (numbermoney < 0) | ||
{ | ||
Textreturn = "ลบ"; | ||
numbermoney = -(numbermoney); | ||
} | ||
|
||
if ((numbermoney / 1000000) > 0) | ||
{ | ||
Textreturn += Convert(numbermoney / 1000000) + "ล้าน"; | ||
numbermoney %= 1000000; | ||
} | ||
if ((numbermoney / 100000) > 0) | ||
{ | ||
Textreturn += Convert(numbermoney / 100000) + "แสน"; | ||
numbermoney %= 100000; | ||
} | ||
if ((numbermoney / 10000) > 0) | ||
{ | ||
Textreturn += Convert(numbermoney / 10000) + "หมื่น"; | ||
numbermoney %= 10000; | ||
} | ||
if ((numbermoney / 1000) > 0) | ||
{ | ||
Textreturn += Convert(numbermoney / 1000) + "พัน"; | ||
numbermoney %= 1000; | ||
} | ||
|
||
if ((numbermoney / 100) > 0) | ||
{ | ||
Textreturn += Convert(numbermoney / 100) + "ร้อย"; | ||
numbermoney %= 100; | ||
} | ||
|
||
if (numbermoney > 0) | ||
{ | ||
if (Textreturn != "") | ||
{ | ||
Textreturn += ""; | ||
} | ||
|
||
var unitsMap = new[] { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "เเปด", "เก้า", "สิบ", "สิบเอ็ด", "สิบสอง", "สิบสาม", "สิบสี่", "สิบห้า", "สิบหก", "สิบเจ็ด", "สิบเเปด", "สิบเก้า" }; | ||
var tensMap = new[] { "ศูนย์", "สิบ", "ยี่สิบ", "สามสิบ", "สี่สิบ", "ห้าสิบ", "หกสิบ", "เจ็ดสิบ", "แปดสิบ", "เก้าสิบ" }; | ||
|
||
if (numbermoney < 20) | ||
{ | ||
Textreturn += unitsMap[numbermoney]; | ||
} | ||
else | ||
{ | ||
Textreturn += tensMap[numbermoney / 10]; | ||
if ((numbermoney % 10) > 0) | ||
{ | ||
Textreturn += "" + unitsMap[numbermoney % 10]; | ||
} | ||
} | ||
} | ||
|
||
return Textreturn; | ||
} | ||
|
||
public override string ConvertToOrdinal(int number) | ||
{ | ||
return Convert(number); | ||
} | ||
} | ||
} |