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

Adding Croatian NumberToWords #906

Merged
merged 1 commit into from
Apr 1, 2020
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
1 change: 1 addition & 0 deletions src/Humanizer.Tests.Shared/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public void ToOrdinalWords(int number, string words)
[InlineData(11, "en-US", "eleven")]
[InlineData(22, "ar", "اثنان و عشرون")]
[InlineData(40, "ru", "сорок")]
[InlineData(1021, "hr", "tisuću dvadeset jedan")]
public void ToWords_CanSpecifyCultureExplicitly(int number, string culture, string expected)
{
Assert.Equal(expected, number.ToWords(new CultureInfo(culture)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public NumberToWordsConverterRegistry()
Register("sv", new SwedishNumberToWordsConverter());
Register("sr", (culture) => new SerbianCyrlNumberToWordsConverter(culture));
Register("sr-Latn", (culture) => new SerbianNumberToWordsConverter(culture));
Register("hr", (culture) => new CroatianNumberToWordsConverter(culture));
Register("nb", new NorwegianBokmalNumberToWordsConverter());
Register("vi", new VietnameseNumberToWordsConverter());
Register("zh-CN", new ChineseNumberToWordsConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Humanizer.Localisation.NumberToWords
{
internal class CroatianNumberToWordsConverter : GenderlessNumberToWordsConverter
{
private static readonly string[] UnitsMap = { "nula", "jedan", "dva", "tri", "četiri", "pet", "šest", "sedam", "osam", "devet", "deset", "jedanaest", "dvanaest", "trinaest", "četrnaest", "petnaest", "šestnaest", "sedemnaest", "osemnaest", "devetnaest" };
private static readonly string[] TensMap = { "nula", "deset", "dvadeset", "trideset", "četrdeset", "petdeset", "šestdeset", "sedamdeset", "osamdeset", "devetdeset" };

private readonly CultureInfo _culture;

public CroatianNumberToWordsConverter(CultureInfo culture)
{
_culture = culture;
}

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

if (number < 0)
{
return string.Format("- {0}", Convert(-number));
}

var parts = new List<string>();
var billions = number / 1000000000;

if (billions > 0)
{
parts.Add(Part("milijarda", "dvije milijarde", "{0} milijarde", "{0} milijarda", billions));
number %= 1000000000;

if (number > 0)
{
parts.Add(" ");
}
}

var millions = number / 1000000;

if (millions > 0)
{
parts.Add(Part("milijun", "dva milijuna", "{0} milijuna", "{0} milijuna", millions));
number %= 1000000;

if (number > 0)
{
parts.Add(" ");
}
}

var thousands = number / 1000;

if (thousands > 0)
{
parts.Add(Part("tisuću", "dvije tisuće", "{0} tisuće", "{0} tisuća", thousands));
number %= 1000;

if (number > 0)
{
parts.Add(" ");
}
}

var hundreds = number / 100;

if (hundreds > 0)
{
parts.Add(Part("sto", "dvijesto", "{0}sto", "{0}sto", hundreds));
number %= 100;

if (number > 0)
{
parts.Add(" ");
}
}

if (number > 0)
{
if (number < 20)
{
parts.Add(UnitsMap[number]);
}
else
{
parts.Add(TensMap[number / 10]);
var units = number % 10;

if (units > 0)
{
parts.Add(string.Format(" {0}", UnitsMap[units]));
}
}
}

return string.Join("", parts);
}

public override string ConvertToOrdinal(int number)
{
//TODO: In progress
return number.ToString(_culture);
}

private string Part(string singular, string dual, string trialQuadral, string plural, int number)
{
switch (number)
{
case 1:
return singular;
case 2:
return dual;
case 3:
case 4:
return string.Format(trialQuadral, Convert(number));
default:
return string.Format(plural, Convert(number));
}
}
}
}