using System.Globalization;
static string DollarsToWords(decimal value)
if (value == 0) return "zero dollars";
var valStr = value.ToString(CultureInfo.InvariantCulture);
var splitOnDecimalVal = valStr.Split(new[] {'.'});
if (splitOnDecimalVal.Length == 2)
var dollarAmtString = ConvertDollarAmountToWords(Convert.ToInt32(splitOnDecimalVal[0]), true);
var centAmtString = ConvertCentAmountToWords(Convert.ToInt32(splitOnDecimalVal[1]));
if (!string.IsNullOrWhiteSpace(dollarAmtString))
word = $"{dollarAmtString}".Trim();
word = AppendAndIfNeeded(centAmtString, word);
word += $" {centAmtString}";
word = $"{centAmtString}";
var centAmtString = ConvertCentAmountToWords(Convert.ToInt32(splitOnDecimalVal[0]));
return word.Trim().ToLowerInvariant();
private static string ConvertCentAmountToWords(int value)
if (value <= 0) return string.Empty;
var word = GetEquivalentWordForInt(value);
return $"{word} {(value == 1 ? "cent" : "cents")}";
private static string ConvertDollarAmountToWords(int value, bool hasDecimals)
if (value <= 0 && hasDecimals) return string.Empty;
var word = GetEquivalentWordForInt(value);
return $"{word} {(value == 1 ? "dollar" : "dollars")}";
private static string GetEquivalentWordForInt(int value)
var tensVal = value % 100;
tens = ConvertIntToWords(tensVal.ToString());
var hundredVal = value / 100;
hundreds = ConvertIntToWords(hundredVal.ToString());
if (!string.IsNullOrWhiteSpace(hundreds))
word += $"{hundreds} hundred";
if (!string.IsNullOrWhiteSpace(tens))
word = AppendAndIfNeeded(tens, word);
var onesVal = tensVal % 10;
var tenVal = tensVal / 10;
var hundredVal = (value / 100);
hundreds = ConvertIntToWords(hundredVal.ToString());
tens = ConvertTensToWords(tenVal.ToString());
var ones = ConvertIntToWords(onesVal.ToString());
if (!string.IsNullOrWhiteSpace(hundreds))
word += $"{hundreds} hundred";
if (!string.IsNullOrWhiteSpace(tens))
word = AppendAndIfNeeded(tens, word);
if (!string.IsNullOrWhiteSpace(ones))
if (string.IsNullOrWhiteSpace(tens))
word = AppendAndIfNeeded(hundreds, word);
public static string AppendAndIfNeeded(string s, string toBeModified)
if (!string.IsNullOrEmpty(s))
if (!string.IsNullOrEmpty(toBeModified))
toBeModified = $"{toBeModified} and";
private static string ConvertTensToWords(string c)
private static string ConvertIntToWords(string c)
public static void Main()
Test(0.12m, "twelve cents");
Test(10.55m, "ten dollars and fifty five cents");
Test(120, "one hundred and twenty cents");
Test(-120, "one hundred and twenty cents");
Test(999.99m, "nine hundred and ninety nine dollars and ninety nine cents");
Test(999, "nine hundred and ninety nine cents");
Test(100.50m, "one hundred dollars and fifty cents");
Test(101.50m, "one hundred and one dollars and fifty cents");
Test(1.00m, "one dollar");
Test(1.05m, "one dollar and five cents");
Test(1.01m, "one dollar and one cent");
Test(500.00m, "five hundred dollars");
Test(110.10m, "one hundred and ten dollars and ten cents");
Test(0.02m, "two cents");
static void Test(decimal number, string expected)
var actual = DollarsToWords(number);
Console.WriteLine($"{number}: PASS");
Console.WriteLine($"{number}: FAIL");
Console.WriteLine($" Expected: {expected}");
Console.WriteLine($" Actual: {actual}");