Instructions

Test suite

Solution

Given a number from 0 to 999,999,999,999, spell out that number in English.

Handle the basic case of 0 through 99.

If the input to the program is `22`

, then the output should be
`'twenty-two'`

.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

- 0
- 14
- 50
- 98
- -1
- 100

If you're on a Mac, shell out to Mac OS X's `say`

program to talk out
loud.

Implement breaking a number up into chunks of thousands.

So `1234567890`

should yield a list like 1, 234, 567, and 890, while the
far simpler `1000`

should yield just 1 and 0.

The program must also report any values that are out of range.

Now handle inserting the appropriate scale word between those chunks.

So `1234567890`

should yield `'1 billion 234 million 567 thousand 890'`

The program must also report any values that are out of range. It's fine to stop at "trillion".

Put it all together to get nothing but plain English.

`12345`

should give `twelve thousand three hundred forty-five`

.

The program must also report any values that are out of range.

Use *and* (correctly) when spelling out the number in English:

- 14 becomes "fourteen".
- 100 becomes "one hundred".
- 120 becomes "one hundred and twenty".
- 1002 becomes "one thousand and two".
- 1323 becomes "one thousand three hundred and twenty-three".

A variation on JavaRanch CattleDrive, exercise 4a http://www.javaranch.com/say.jsp

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
// This file was auto-generated based on version 1.1.0 of the canonical data.
using System;
using Xunit;
public class SayTest
{
[Fact]
public void Zero()
{
Assert.Equal("zero", Say.InEnglish(0));
}
[Fact(Skip = "Remove to run test")]
public void One()
{
Assert.Equal("one", Say.InEnglish(1));
}
[Fact(Skip = "Remove to run test")]
public void Fourteen()
{
Assert.Equal("fourteen", Say.InEnglish(14));
}
[Fact(Skip = "Remove to run test")]
public void Twenty()
{
Assert.Equal("twenty", Say.InEnglish(20));
}
[Fact(Skip = "Remove to run test")]
public void Twenty_two()
{
Assert.Equal("twenty-two", Say.InEnglish(22));
}
[Fact(Skip = "Remove to run test")]
public void One_hundred()
{
Assert.Equal("one hundred", Say.InEnglish(100));
}
[Fact(Skip = "Remove to run test")]
public void One_hundred_twenty_three()
{
Assert.Equal("one hundred twenty-three", Say.InEnglish(123));
}
[Fact(Skip = "Remove to run test")]
public void One_thousand()
{
Assert.Equal("one thousand", Say.InEnglish(1000));
}
[Fact(Skip = "Remove to run test")]
public void One_thousand_two_hundred_thirty_four()
{
Assert.Equal("one thousand two hundred thirty-four", Say.InEnglish(1234));
}
[Fact(Skip = "Remove to run test")]
public void One_million()
{
Assert.Equal("one million", Say.InEnglish(1000000));
}
[Fact(Skip = "Remove to run test")]
public void One_million_two_thousand_three_hundred_forty_five()
{
Assert.Equal("one million two thousand three hundred forty-five", Say.InEnglish(1002345));
}
[Fact(Skip = "Remove to run test")]
public void One_billion()
{
Assert.Equal("one billion", Say.InEnglish(1000000000));
}
[Fact(Skip = "Remove to run test")]
public void A_big_number()
{
Assert.Equal("nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three", Say.InEnglish(987654321123));
}
[Fact(Skip = "Remove to run test")]
public void Numbers_below_zero_are_out_of_range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(-1));
}
[Fact(Skip = "Remove to run test")]
public void Numbers_above_999_999_999_999_are_out_of_range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(1000000000000));
}
}
```

```
using System;
using System.Linq;
using System.Collections.Generic;
public static class Say
{
public static string InEnglish(long number)
{
if(number < 0 || number > Math.Pow(10, 12) - 1)
throw new ArgumentOutOfRangeException();
if (number == 0)
return "zero";
return UnderTrillion(number);
}
private static (long, long) Divide (long number, int power)
{
var devisor = (long)Math.Pow(10.0, power);
return (number / devisor, number % devisor);
}
private static readonly Dictionary<long, string> underTwentyNames =
new (long number, string name)[]
{ (1, "one"), (2, "two"), (3, "three"), (4, "four"), (5, "five"),
(6, "six"), (7, "seven"), (8, "eight"), (9, "nine"), (10, "ten"),
(11, "eleven"), (12, "twelve"), (13, "thirteen"), (14, "fourteen"), (15, "fifteen"),
(16, "sixteen"), (17, "seventeen"), (18, "eighteen"), (19, "nineteen") }
.ToDictionary(o => o.number, o => o.name);
private static readonly Dictionary<long, string> underHundredPrefixes =
new (long number, string name)[]
{ (2, "twenty-"), (3, "thirty-"), (4, "forty-"), (5, "fifty-"),
(6, "sixty-"), (7, "seventy-"), (8, "eighty-"), (9, "ninety-") }
.ToDictionary(o => o.number, o => o.name);
private static string UnderTwenty(long number)
=> underTwentyNames.GetValueOrDefault(number, "");
private static string UnderHundred(long number)
{
var (tens, remainder) = Divide(number, 1);
if (tens < 2)
return UnderTwenty(number);
var prefix = underHundredPrefixes.GetValueOrDefault(tens, "");
var name = UnderTwenty(remainder);
return $"{prefix}{name}".TrimEnd('-');
}
private static string UnderThousand(long number)
{
var (hundreds, remainder) = Divide(number, 2);
if(hundreds == 0)
return UnderHundred(number);
return $"{UnderTwenty(hundreds)} hundred {UnderHundred(remainder)}".Trim();
}
private static string UnderMillion(long number)
{
var (thousands, remainder) = Divide(number, 3);
if(thousands == 0)
return UnderThousand(number);
return $"{UnderThousand(thousands)} thousand {UnderThousand(remainder)}".Trim();
}
private static string UnderBillion(long number)
{
var (millions, remainder) = Divide(number, 6);
if(millions == 0)
return UnderMillion(number);
return $"{UnderThousand(millions)} million {UnderMillion(remainder)}".Trim();
}
private static string UnderTrillion(long number)
{
var (billions, remainder) = Divide(number, 9);
if(billions == 0)
return UnderBillion(number);
return $"{UnderThousand(billions)} billion {UnderBillion(remainder)}".Trim();
}
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,091 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments