Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1196 - Support long for FarsiNumberToWordsConverter #1197

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class NumberToWordsTests
[InlineData(10000000, "ده میلیون")]
[InlineData(100000000, "صد میلیون")]
[InlineData(1000000000, "یک میلیارد")]
[InlineData(1000000000000, "یک بیلیون")]
[InlineData(1000000000000000, "یک بیلیارد")]
[InlineData(1000000000000000000, "یک تریلیون")]
[InlineData(111, "صد و یازده")]
[InlineData(1111, "یک هزار و صد و یازده")]
[InlineData(111111, "صد و یازده هزار و صد و یازده")]
Expand All @@ -33,7 +36,8 @@ public class NumberToWordsTests
[InlineData(12345678, "دوازده میلیون و سیصد و چهل و پنج هزار و ششصد و هفتاد و هشت")]
[InlineData(123456789, "صد و بیست و سه میلیون و چهارصد و پنجاه و شش هزار و هفتصد و هشتاد و نه")]
[InlineData(1234567890, "یک میلیارد و دویست و سی و چهار میلیون و پانصد و شصت و هفت هزار و هشتصد و نود")]
public void ToWordsFarsi(int number, string expected)
[InlineData(long.MaxValue, "نه تریلیون و دویست و بیست و سه بیلیارد و سیصد و هفتاد و دو بیلیون و سی و شش میلیارد و هشتصد و پنجاه و چهار میلیون و هفتصد و هفتاد و پنج هزار و هشتصد و هفت")]
public void ToWordsFarsi(long number, string expected)
{
Assert.Equal(expected, number.ToWords());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ internal class FarsiNumberToWordsConverter : GenderlessNumberToWordsConverter
private static readonly string[] FarsiTensMap = { "صفر", "ده", "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود" };
private static readonly string[] FarsiUnitsMap = { "صفر", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه", "ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده" };

public override string Convert(long input)
public override string Convert(long number)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number < 0)
{
return string.Format("منفی {0}", Convert(-number));
Expand All @@ -27,12 +21,15 @@ public override string Convert(long input)
return "صفر";
}

var farsiGroupsMap = new Dictionary<int, Func<int, string>>
var farsiGroupsMap = new Dictionary<long, Func<long, string>>
{
{(int)Math.Pow(10, 9), n => string.Format("{0} میلیارد", Convert(n)) },
{(int)Math.Pow(10, 6), n => string.Format("{0} میلیون", Convert(n)) },
{(int)Math.Pow(10, 3), n => string.Format("{0} هزار", Convert(n)) },
{(int)Math.Pow(10, 2), n => FarsiHundredsMap[n]}
{(long)Math.Pow(10, 18), n => string.Format("{0} تریلیون", Convert(n)) },
{(long)Math.Pow(10, 15), n => string.Format("{0} بیلیارد", Convert(n)) },
{(long)Math.Pow(10, 12), n => string.Format("{0} بیلیون", Convert(n)) },
{(long)Math.Pow(10, 9), n => string.Format("{0} میلیارد", Convert(n)) },
{(long)Math.Pow(10, 6), n => string.Format("{0} میلیون", Convert(n)) },
{(long)Math.Pow(10, 3), n => string.Format("{0} هزار", Convert(n)) },
{(long)Math.Pow(10, 2), n => FarsiHundredsMap[n]}
};

var parts = new List<string>();
Expand Down