-
Notifications
You must be signed in to change notification settings - Fork 964
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dependabot/nuget/coverlet.collector-1.1.0
- Loading branch information
Showing
4 changed files
with
299 additions
and
6 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/Humanizer.Tests.Shared/Localisation/sv/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,79 @@ | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Localisation.sv | ||
{ | ||
[UseCulture("sv-SE")] | ||
public class NumberToWordsTests | ||
{ | ||
[Theory] | ||
[InlineData(0, "noll")] | ||
[InlineData(1, "ett")] | ||
[InlineData(2, "två")] | ||
[InlineData(3, "tre")] | ||
[InlineData(4, "fyra")] | ||
[InlineData(5, "fem")] | ||
[InlineData(6, "sex")] | ||
[InlineData(7, "sju")] | ||
[InlineData(8, "åtta")] | ||
[InlineData(9, "nio")] | ||
[InlineData(10, "tio")] | ||
[InlineData(20, "tjugo")] | ||
[InlineData(30, "trettio")] | ||
[InlineData(40, "fyrtio")] | ||
[InlineData(50, "femtio")] | ||
[InlineData(60, "sextio")] | ||
[InlineData(70, "sjuttio")] | ||
[InlineData(80, "åttio")] | ||
[InlineData(90, "nittio")] | ||
[InlineData(100, "hundra")] | ||
[InlineData(200, "tvåhundra")] | ||
[InlineData(1000, "ett tusen")] | ||
[InlineData(10000, "tio tusen")] | ||
[InlineData(100000, "hundra tusen")] | ||
[InlineData(1000000, "en miljon")] | ||
[InlineData(10000000, "tio miljoner")] | ||
[InlineData(100000000, "hundra miljoner")] | ||
[InlineData(1000000000, "en miljard")] | ||
[InlineData(2000000000, "två miljarder")] | ||
[InlineData(122, "hundratjugotvå")] | ||
[InlineData(3501, "tre tusen femhundraett")] | ||
[InlineData(111, "hundraelva")] | ||
[InlineData(1112, "ett tusen hundratolv")] | ||
[InlineData(11213, "elva tusen tvåhundratretton")] | ||
public void ToWords(long number, string expected) | ||
{ | ||
Assert.Equal(expected, number.ToWords()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, "nollte")] | ||
[InlineData(1, "första")] | ||
[InlineData(2, "andra")] | ||
[InlineData(3, "tredje")] | ||
[InlineData(4, "fjärde")] | ||
[InlineData(5, "femte")] | ||
[InlineData(6, "sjätte")] | ||
[InlineData(7, "sjunde")] | ||
[InlineData(8, "åttonde")] | ||
[InlineData(9, "nionde")] | ||
[InlineData(10, "tionde")] | ||
[InlineData(20, "tjugonde")] | ||
[InlineData(30, "trettionde")] | ||
[InlineData(40, "fyrtionde")] | ||
[InlineData(50, "femtionde")] | ||
[InlineData(60, "sextionde")] | ||
[InlineData(70, "sjuttionde")] | ||
[InlineData(80, "åttionde")] | ||
[InlineData(90, "nittionde")] | ||
[InlineData(100, "hundrade")] | ||
[InlineData(200, "tvåhundrade")] | ||
[InlineData(1000, "ett tusende")] | ||
[InlineData(10000, "tio tusende")] | ||
[InlineData(100000, "hundra tusende")] | ||
[InlineData(1000000, "en miljonte")] | ||
public void ToOrdinalWords(int number, string expected) | ||
{ | ||
Assert.Equal(expected, number.ToOrdinalWords()); | ||
} | ||
} | ||
} |
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
219 changes: 219 additions & 0 deletions
219
src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.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,219 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Humanizer.Localisation.NumberToWords | ||
{ | ||
internal class SwedishNumberToWordsConverter : GenderlessNumberToWordsConverter | ||
{ | ||
private static readonly string[] UnitsMap = { "noll", "ett", "två", "tre", "fyra", "fem", "sex", "sju", "åtta", "nio", "tio", "elva", "tolv", "tretton", "fjorton", "femton", "sexton", "sjutton", "arton", "nitton" }; | ||
private static readonly string[] TensMap = { "noll", "tio", "tjugo", "trettio", "fyrtio", "femtio", "sextio", "sjuttio", "åttio", "nittio", "hundra" }; | ||
|
||
private class Fact | ||
{ | ||
public int Value { get; set; } | ||
public string Name { get; set; } | ||
public string Prefix { get; set; } | ||
public string Postfix { get; set; } | ||
public bool DisplayOneUnit { get; set; } | ||
public GrammaticalGender Gender { get; set; } = GrammaticalGender.Neuter; | ||
} | ||
|
||
private static readonly Fact[] Hunderds = | ||
{ | ||
new Fact {Value = 1000000000, Name = "miljard", Prefix = " ", Postfix = " ", DisplayOneUnit = true, Gender = GrammaticalGender.Masculine}, | ||
new Fact {Value = 1000000, Name = "miljon", Prefix = " ", Postfix = " ", DisplayOneUnit = true, Gender = GrammaticalGender.Masculine}, | ||
new Fact {Value = 1000, Name = "tusen", Prefix = " ", Postfix = " ", DisplayOneUnit = true}, | ||
new Fact {Value = 100, Name = "hundra", Prefix = "", Postfix = "", DisplayOneUnit = false} | ||
}; | ||
|
||
public string Convert(long input, GrammaticalGender gender) | ||
{ | ||
if (input > Int32.MaxValue || input < Int32.MinValue) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
var number = (int)input; | ||
|
||
if (number == 0) | ||
{ | ||
return UnitsMap[0]; | ||
} | ||
|
||
if (number < 0) | ||
{ | ||
return string.Format("minus {0}", Convert(-number, gender)); | ||
} | ||
|
||
var word = ""; | ||
|
||
foreach (var m in Hunderds) | ||
{ | ||
var divided = number / m.Value; | ||
|
||
if (divided <= 0) | ||
{ | ||
continue; | ||
} | ||
|
||
if (divided == 1 && !m.DisplayOneUnit) | ||
{ | ||
word += m.Name; | ||
} | ||
else | ||
{ | ||
word += Convert(divided, m.Gender) + m.Prefix + m.Name; | ||
} | ||
|
||
// pluralise 1M+ | ||
if (divided > 1 && input >= 1_000_000) | ||
{ | ||
word += "er"; | ||
} | ||
|
||
number %= m.Value; | ||
if (number > 0) | ||
{ | ||
word += m.Postfix; | ||
} | ||
} | ||
|
||
if (number > 0) | ||
{ | ||
if (number < 20) | ||
{ | ||
if (number == 1 && gender == GrammaticalGender.Masculine) | ||
{ | ||
word += "en"; | ||
} | ||
else | ||
{ | ||
word += UnitsMap[number]; | ||
} | ||
} | ||
else | ||
{ | ||
var tens = TensMap[number / 10]; | ||
var unit = number % 10; | ||
if (unit > 0) | ||
{ | ||
var units = UnitsMap[unit]; | ||
word += tens + units; | ||
} | ||
else | ||
{ | ||
word += tens; | ||
} | ||
} | ||
} | ||
|
||
return word; | ||
} | ||
public override string Convert(long input) | ||
{ | ||
return Convert(input, GrammaticalGender.Neuter); | ||
} | ||
|
||
private static string[] ordinalNumbers = new[] | ||
{ | ||
"nollte", | ||
"första", | ||
"andra", | ||
"tredje", | ||
"fjärde", | ||
"femte", | ||
"sjätte", | ||
"sjunde", | ||
"åttonde", | ||
"nionde", | ||
"tionde", | ||
"elfte", | ||
"tolfte", | ||
"trettonde", | ||
"fjortonde", | ||
"femtonde", | ||
"sextonde", | ||
"sjuttonde", | ||
"artonde", | ||
"nittonde", | ||
"tjugonde", | ||
}; | ||
|
||
public override string ConvertToOrdinal(int number) | ||
{ | ||
var word = ""; | ||
|
||
if (number < 0) | ||
{ | ||
return $"minus {ConvertToOrdinal(-number)}"; | ||
} | ||
|
||
if (number <= 20) | ||
{ | ||
return ordinalNumbers[number]; | ||
} | ||
|
||
// 21+ | ||
if (number <= 100) | ||
{ | ||
var tens = TensMap[number / 10]; | ||
var unit = number % 10; | ||
if (unit > 0) | ||
{ | ||
word += tens + ConvertToOrdinal(unit); | ||
} | ||
else if (number == 100) | ||
{ | ||
word += tens + "de"; | ||
} | ||
else { | ||
word += tens + "nde"; | ||
} | ||
|
||
return word; | ||
} | ||
|
||
// 101+ | ||
foreach (var m in Hunderds) | ||
{ | ||
var divided = number / m.Value; | ||
|
||
if (divided <= 0) | ||
{ | ||
continue; | ||
} | ||
|
||
if (divided == 1 && !m.DisplayOneUnit) | ||
{ | ||
word += m.Name; | ||
} | ||
else | ||
{ | ||
word += Convert(divided, m.Gender) + m.Prefix + m.Name; | ||
} | ||
|
||
// suffix -de/-te | ||
if (divided > 0) | ||
{ | ||
switch (number) | ||
{ | ||
case 1_000_000: | ||
word += "te"; | ||
break; | ||
default: | ||
word += "de"; | ||
break; | ||
} | ||
} | ||
|
||
number %= m.Value; | ||
if (number > 0) | ||
{ | ||
word += m.Postfix; | ||
} | ||
} | ||
|
||
return word; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.