Skip to content

Commit

Permalink
Merge pull request #1032 from czbas23/master
Browse files Browse the repository at this point in the history
Support Thai formatter and number to word
  • Loading branch information
clairernovotny authored Apr 23, 2021
2 parents 8b8a7af + 32b1cfd commit ecb0523
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Humanizer.Tests/Localisation/th-TH/DateHumanizeTests.cs
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 src/Humanizer.Tests/Localisation/th-TH/NumberToWordsTests.cs
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());
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Configuration/FormatterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public FormatterRegistry() : base(new DefaultFormatter("en-US"))
RegisterDefaultFormatter("zh-CN");
RegisterDefaultFormatter("zh-Hans");
RegisterDefaultFormatter("zh-Hant");
RegisterDefaultFormatter("th-TH");
}

private void RegisterDefaultFormatter(string localeCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public NumberToWordsConverterRegistry()
Register("hy", new ArmenianNumberToWordsConverter());
Register("az", new AzerbaijaniNumberToWordsConverter());
Register("ja", new JapaneseNumberToWordsConverter());
Register("th-TH", new ThaiNumberToWordsConverter());
}
}
}
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);
}
}
}

0 comments on commit ecb0523

Please sign in to comment.