Skip to content

Commit

Permalink
Merge pull request #78 from hazzik/billions
Browse files Browse the repository at this point in the history
NumberToWordsExtension.ToWords add ability to convert billions
  • Loading branch information
MehdiK committed Feb 12, 2014
2 parents 72abd8f + 2003e39 commit 3ebfe66
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/Humanizer.Tests/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests
{
public class NumberToWordsTests
{
[Fact]
public void ToWords()
[InlineData(1, "one")]
[InlineData(10, "ten")]
[InlineData(11, "eleven")]
[InlineData(122, "one hundred and twenty-two")]
[InlineData(3501, "three thousand five hundred and one")]
[InlineData(100, "one hundred")]
[InlineData(1000, "one thousand")]
[InlineData(100000, "one hundred thousand")]
[InlineData(1000000, "one million")]
[InlineData(10000000, "ten million")]
[InlineData(100000000, "one hundred million")]
[InlineData(1000000000, "one billion")]
[InlineData(111, "one hundred and eleven")]
[InlineData(1111, "one thousand one hundred and eleven")]
[InlineData(111111, "one hundred and eleven thousand one hundred and eleven")]
[InlineData(1111111, "one million one hundred and eleven thousand one hundred and eleven")]
[InlineData(11111111, "eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(111111111, "one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(1111111111, "one billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(123, "one hundred and twenty-three")]
[InlineData(1234, "one thousand two hundred and thirty-four")]
[InlineData(12345, "twelve thousand three hundred and forty-five")]
[InlineData(123456, "one hundred and twenty-three thousand four hundred and fifty-six")]
[InlineData(1234567, "one million two hundred and thirty-four thousand five hundred and sixty-seven")]
[InlineData(12345678, "twelve million three hundred and forty-five thousand six hundred and seventy-eight")]
[InlineData(123456789, "one hundred and twenty-three million four hundred and fifty-six thousand seven hundred and eighty-nine")]
[InlineData(1234567890, "one billion two hundred and thirty-four million five hundred and sixty-seven thousand eight hundred and ninety")]
[Theory]
public void Test(int number, string expected)
{
Assert.Equal("one", 1.ToWords());
Assert.Equal("ten", 10.ToWords());
Assert.Equal("eleven", 11.ToWords());
Assert.Equal("one hundred and twenty-two", 122.ToWords());
Assert.Equal("three thousand five hundred and one", 3501.ToWords());
}

[Fact]
public void RoundNumbersHaveNoSpaceAtTheEnd()
{
Assert.Equal("one hundred", 100.ToWords());
Assert.Equal("one thousand", 1000.ToWords());
Assert.Equal("one hundred thousand", 100000.ToWords());
Assert.Equal("one million", 1000000.ToWords());
Assert.Equal(expected, number.ToWords());
}
}
}
6 changes: 6 additions & 0 deletions src/Humanizer/NumberToWordsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ private static string ToEnglishWords(int number)

var parts = new List<string>();

if ((number / 1000000000) > 0)
{
parts.Add(string.Format("{0} billion", ToWords(number / 1000000000)));
number %= 1000000000;
}

if ((number / 1000000) > 0)
{
parts.Add(string.Format("{0} million", ToWords(number / 1000000)));
Expand Down

0 comments on commit 3ebfe66

Please sign in to comment.