Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print value of number (int) spelled out

Tags:

c#

is there an out-of-box way to spell out an int in C#? For example if I have:

int a = 53;

I want to print:

"fifty three"

not

"53"

If not, does anybody have any examples on how to accomplis this?

Thanks!

like image 615
Ricardo Villamil Avatar asked Apr 08 '10 14:04

Ricardo Villamil


3 Answers

You have to write that code yourself. If had to guess, I'd say it's not in the framework because it'd be near impossible to localize (there's much more to this than just the names of digits: word order, hyphen rules, etc).

This brings back memories because it was an assignment from my very first programming course back in college, so it shouldn't be too hard for you to write one just in English.

like image 198
Joel Coehoorn Avatar answered Sep 27 '22 15:09

Joel Coehoorn


Here is code for converting number to words.

using System;
namespace WpfApplication1
{
 public class NumberToEnglish
  {
   public String changeNumericToWords(double numb)
   {
    String num = numb.ToString();
    return changeToWords(num, false);
   }
   public String changeCurrencyToWords(String numb)
   {
    return changeToWords(numb, true);
   }
   public String changeNumericToWords(String numb)
   {
    return changeToWords(numb, false);
   }
   public String changeCurrencyToWords(double numb)
   {
    return changeToWords(numb.ToString(), true);
   }
   private String changeToWords(String numb, bool isCurrency)
   {
   String val = "", wholeNo = numb, points = "", andStr = "", pointStr="";
   String endStr = (isCurrency) ? ("Only") : ("");
   try
   {
    int decimalPlace = numb.IndexOf(".");
    if (decimalPlace > 0)
    {
     wholeNo = numb.Substring(0, decimalPlace);
     points = numb.Substring(decimalPlace+1);
     if (Convert.ToInt32(points) > 0)
     {
      andStr = (isCurrency)?("and"):("point");// just to separate whole numbers from > points/cents
      endStr = (isCurrency) ? ("Cents "+endStr) : ("");
      pointStr = translateCents(points);
     }
    }
    val = String.Format("{0} {1}{2} {3}",translateWholeNumber(wholeNo).Trim(),andStr,pointStr,endStr);
   }
   catch { ;}
   return val;
  }
   private String translateWholeNumber(String number)
   {
    string word = "";
    try
    {
     bool beginsZero = false;//tests for 0XX
     bool isDone = false;//test if already translated
     double dblAmt = (Convert.ToDouble(number));
     //if ((dblAmt > 0) && number.StartsWith("0"))
     if (dblAmt > 0)
     {//test for zero or digit zero in a nuemric
      beginsZero = number.StartsWith("0");

      int numDigits = number.Length;
      int pos = 0;//store digit grouping
      String place = "";//digit grouping name:hundres,thousand,etc...
      switch (numDigits)
      {
       case 1://ones' range
        word = ones(number);
        isDone = true;
        break;
       case 2://tens' range
        word = tens(number);
        isDone = true;
        break;
       case 3://hundreds' range
        pos = (numDigits % 3) + 1;
        place = " Hundred ";
        break;
       case 4://thousands' range
       case 5:
       case 6:
        pos = (numDigits % 4) + 1;
        place = " Thousand ";
        break;
       case 7://millions' range
       case 8:
       case 9:
        pos = (numDigits % 7) + 1;
        place = " Million ";
        break;
       case 10://Billions's range
        pos = (numDigits % 10) + 1;
        place = " Billion ";
        break;
       //add extra case options for anything above Billion...
       default:
        isDone = true;
        break;
      }
      if (!isDone)
      {//if transalation is not done, continue...(Recursion comes in now!!)
       word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
       //check for trailing zeros
       if (beginsZero) word = " and " + word.Trim();
      }
      //ignore digit grouping names
      if (word.Trim().Equals(place.Trim())) word = "";
     }
    }
    catch { ;}
    return word.Trim();
   }
   private String tens(String digit)
   {
    int digt = Convert.ToInt32(digit);
    String name = null;
    switch (digt)
    {
     case 10:
      name = "Ten";
      break;
     case 11:
      name = "Eleven";
      break;
     case 12:
      name = "Twelve";
      break;
     case 13:
      name = "Thirteen";
      break;
     case 14:
      name = "Fourteen";
      break;
     case 15:
      name = "Fifteen";
      break;
     case 16:
      name = "Sixteen";
      break;
     case 17:
      name = "Seventeen";
      break;
     case 18:
      name = "Eighteen";
      break;
     case 19:
      name = "Nineteen";
      break;
     case 20:
      name = "Twenty";
      break;
     case 30:
      name = "Thirty";
      break;
     case 40:
      name = "Fourty";
      break;
     case 50:
      name = "Fifty";
      break;
     case 60:
      name = "Sixty";
      break;
     case 70:
      name = "Seventy";
      break;
     case 80:
      name = "Eighty";
      break;
     case 90:
      name = "Ninety";
      break;
     default:
      if (digt > 0)
      {
       name = tens(digit.Substring(0, 1) + "0") + "" + ones(digit.Substring(1));
      }
      break;
    }
    return name;
   }
   private String ones(String digit)
   {
    int digt = Convert.ToInt32(digit);
    String name = "";
    switch (digt)
    {
     case 1:
      name = "One";
      break;
     case 2:
      name = "Two";
      break;
     case 3:
      name = "Three";
      break;
     case 4:
      name = "Four";
      break;
     case 5:
      name = "Five";
      break;
     case 6:
      name = "Six";
      break;
     case 7:
      name = "Seven";
      break;
     case 8:
      name = "Eight";
      break;
     case 9:
      name = "Nine";
      break;
    }
    return name;
   }
   private String translateCents(String cents)
   {
    String cts = "", digit = "", engOne = "";
     for (int i = 0; i < cents.Length; i++)
     {
      digit = cents[i].ToString();
      if (digit.Equals("0"))
      {
       engOne = "Zero";
      }
      else
      {
       engOne = ones(digit);
      }
      cts += " " + engOne;
     }
    return cts;
   }
  }
 }

create a class file in your project, copy this code to your class file. Change the namespace to your project namespace.

create obj of that class something like this

NumberToEnglish objnumber = new NumberToEnglish();

and with objnumber.changeNumericToWords(100);

with that function you will get number into word.

source: http://social.msdn.microsoft.com/Forums/en/wpf/thread/42b5bb54-dfd6-4c5b-8d51-82e5fc29f8e8 author: Hiran Repakula

He uses instance method, but you can make these methods static.

like image 26
XpiritO Avatar answered Sep 27 '22 16:09

XpiritO


Here's an additional way, just for kicks.

public static class NumericSpelling
{
    private const long Quadrillion = Trillion * 1000;
    private const long Trillion = Billion * 1000;
    private const long Billion = Million * 1000;
    private const long Million = Thousand * 1000;
    private const long Thousand = Hundred * 10;
    private const long Hundred = 100;

    public static string ToVerbal(this int value) { return ToVerbal((long)value); }

    public static string ToVerbal(this long value)
    {
        if (value == 0) return "zero";

        if (value < 0)
        {
            return "negative " + ToVerbal(Math.Abs(value));
        }

        System.Text.StringBuilder builder = new StringBuilder();

        int unit = 0;

        if (value >= Quadrillion)
        {
            unit = (int)(value / Quadrillion);
            value -= unit * Quadrillion;

            builder.AppendFormat("{0}{1} quadrillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Trillion)
        {
            unit = (int)(value / Trillion);
            value -= unit * Trillion;

            builder.AppendFormat("{0}{1} trillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Billion)
        {
            unit = (int)(value / Billion);
            value -= unit * Billion;

            builder.AppendFormat("{0}{1} billion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Million)
        {
            unit = (int)(value / Million);
            value -= unit * Million;

            builder.AppendFormat("{0}{1} million", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Thousand)
        {
            unit = (int)(value / Thousand);
            value -= unit * Thousand;

            builder.AppendFormat("{0}{1} thousand", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (value >= Hundred)
        {
            unit = (int)(value / Hundred);
            value -= unit * Hundred;

            builder.AppendFormat("{0}{1} hundred", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
        }

        if (builder.Length > 0 && value > 0) builder.AppendFormat(" and");

        if (value >= 90)
        {
            value -= 90;

            builder.AppendFormat("{0}ninety", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 80)
        {
            value -= 80;

            builder.AppendFormat("{0}eighty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 70)
        {
            value -= 70;

            builder.AppendFormat("{0}seventy", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 60)
        {
            value -= 60;

            builder.AppendFormat("{0}sixty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 50)
        {
            value -= 50;

            builder.AppendFormat("{0}fifty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 40)
        {
            value -= 40;

            builder.AppendFormat("{0}forty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 30)
        {
            value -= 30;

            builder.AppendFormat("{0}thirty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value >= 20)
        {
            value -= 20;

            builder.AppendFormat("{0}twenty", builder.Length > 0 ? " " : string.Empty);
        }

        if (value == 19) builder.AppendFormat("{0}nineteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 18) builder.AppendFormat("{0}eighteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 17) builder.AppendFormat("{0}seventeen", builder.Length > 0 ? " " : string.Empty);
        if (value == 16) builder.AppendFormat("{0}sixteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 15) builder.AppendFormat("{0}fifteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 14) builder.AppendFormat("{0}fourteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 13) builder.AppendFormat("{0}thirteen", builder.Length > 0 ? " " : string.Empty);
        if (value == 12) builder.AppendFormat("{0}twelve", builder.Length > 0 ? " " : string.Empty);
        if (value == 11) builder.AppendFormat("{0}eleven", builder.Length > 0 ? " " : string.Empty);
        if (value == 10) builder.AppendFormat("{0}ten", builder.Length > 0 ? " " : string.Empty);
        if (value == 9) builder.AppendFormat("{0}nine", builder.Length > 0 ? " " : string.Empty);
        if (value == 8) builder.AppendFormat("{0}eight", builder.Length > 0 ? " " : string.Empty);
        if (value == 7) builder.AppendFormat("{0}seven", builder.Length > 0 ? " " : string.Empty);
        if (value == 6) builder.AppendFormat("{0}six", builder.Length > 0 ? " " : string.Empty);
        if (value == 5) builder.AppendFormat("{0}five", builder.Length > 0 ? " " : string.Empty);
        if (value == 4) builder.AppendFormat("{0}four", builder.Length > 0 ? " " : string.Empty);
        if (value == 3) builder.AppendFormat("{0}three", builder.Length > 0 ? " " : string.Empty);
        if (value == 2) builder.AppendFormat("{0}two", builder.Length > 0 ? " " : string.Empty);
        if (value == 1) builder.AppendFormat("{0}one", builder.Length > 0 ? " " : string.Empty);

        return builder.ToString();
    }
}

Input:

int first = 10447;
long second = 10576749323475;
int third = 0;
int fourth = -1095;
int fifth = 100;
int sixth = 102;
int seventh = 10004;
int eight = 100025;

Console.WriteLine(first.ToVerbal());
Console.WriteLine(second.ToVerbal());
Console.WriteLine(third.ToVerbal());
Console.WriteLine(fourth.ToVerbal());
Console.WriteLine(fifth.ToVerbal());
Console.WriteLine(sixth.ToVerbal());
Console.WriteLine(seventh.ToVerbal());
Console.WriteLine(eight.ToVerbal());

Output:

ten thousand, four hundred and forty seven
ten trillion, five hundred and seventy six billion, seven hundred and forty nine million, three hundred and twenty three thousand, four hundred and seventy five
zero
negative one thousand and ninety five
one hundred
one hundred and two
ten thousand and four
one hundred thousand and twenty five
like image 43
Adam Robinson Avatar answered Sep 27 '22 17:09

Adam Robinson