Click here to Skip to main content
16,005,467 members
Articles / Programming Languages / C#
Tip/Trick

Integer to Numeral

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 May 2012CPOL 16.8K   5   13
Converts any integer (32-bit) into its numeral (string)

Introduction

This code snippet will convert any integer (32-bit sized) into its numeral value (string).

Example: 1 returns "one". 2356 returns "two thousand three hundred and fifty-six". -2147483647 returns "minus two billion one hundred and forty-seven million four hundred and eighty-three thousand six hundred and forty-seven"

Using the code 

C#
//--Use
//public static string IntToNum( int i )
//
//Ex:
//
using Numerals;

namespace SomeNamespace
{
    class SomeClass
    {
        string s = Numeral.IntToNum( 5691 );
    }
}
C++
using System;

namespace Numerals
{
    public class Numeral
    {
        private static string Base(int i)
        {
            switch (i)
            {
                case 0: return "zero";
                case 1: return "one";
                case 2: return "two";
                case 3: return "three";
                case 4: return "four";
                case 5: return "five";
                case 6: return "six";
                case 7: return "seven";
                case 8: return "eight";
                case 9: return "nine";
                case 10: return "ten";
                case 11: return "eleven";
                case 12: return "twelve";
                case 13: return "thirteen";
                case 14: return "fourteen";
                case 15: return "fifteen";
                case 16: return "sixteen";
                case 17: return "seventeen";
                case 18: return "eighteen";
                case 19: return "nineteen";
                case 20: return "twenty";
                case 30: return "thirty";
                case 40: return "forty";
                case 50: return "fifty";
                case 60: return "sixty";
                case 70: return "seventy";
                case 80: return "eighty";
                case 90: return "ninety";
                case 100: return "hundred";
                case 1000: return "thousand";
                case 1000000: return "million";
                case 1000000000: return "billion";
            }
            return "";
        }
        //================================================================
        public static string IntToNum(int number)
        {
            if (number >= 0)
            {
                //Billion
                if (number >= 1000000000)
                {
                    int round = number / 1000000000;
                    if (number - (round * 1000000000) != 0)
                        return IntToNum(round) + " " + Base(1000000000) + " " + IntToNum(number - (round * 1000000000));
                    else
                        return IntToNum(round) + " " + Base(1000000000);
                }

                //Million
                if (number >= 1000000)
                {
                    int round = number / 1000000;
                    if (number - (round * 1000000) != 0)
                        return IntToNum(round) + " " + Base(1000000) + " " + IntToNum(number - (round * 1000000));
                    else
                        return IntToNum(round) + " " + Base(1000000);
                }

                //Ten thousand
                if (number >= 10000)
                {
                    int round = number / 1000;
                    if (number - (round * 1000) != 0)
                        return IntToNum(round) + " " + Base(1000) + " " + IntToNum(number - (round * 1000));
                    else
                        return IntToNum(round) + " " + Base(1000);
                }

                //Thousand
                if (number >= 1000)
                {
                    int round = number / 1000;
                    if (number - (round * 1000) != 0)
                        return Base(round) + " " + Base(1000) + " " + IntToNum(number - (round * 1000));
                    else
                        return Base(round) + " " + Base(1000);
                }

                //Hundred
                if (number >= 100)
                {
                    int round = number / 100;
                    if (number - (round * 100) != 0)
                        return Base(round) + " " + Base(100) + " and " + IntToNum(number - (round * 100));
                    else
                        return Base(round) + " " + Base(100);
                }

                //Over twenty
                if (number > 20)
                {
                    int round = number / 10;
                    if (number - (round * 10) != 0)
                        return Base(round * 10) + "-" + Base(number - (round * 10));
                    else
                        return Base(round * 10);
                }

                //Less than twenty
                if (number <= 20) return Base(number);
            }
            else if (number < 0)
                return "minus " + IntToNum(Math.Abs(number));
            return "";
        }
        //================================================================
    }
}

Points of Interest

One of my first projects Big Grin | :-D Programming is fun ^^. I guess I could use a loop in IntToNum() to make it faster, but that's for later.

History

1.0 (14th May 2012) - Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sweden Sweden
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
Questionflawed logic? Pin
Daniel Gidman14-May-12 8:09
professionalDaniel Gidman14-May-12 8:09 
created a unit test for this after converting to long use versus int use. your logic is flawed somewhere I think.

90736213576 resulted in ninety billion seven hundred thirty-six million twenty-one three thousand five hundred seventy-six
should of resulted in ninety billion seven hundred thirty-six million two hundred thirteen thousand five hundred seventy-six

bolded the difference

I think if you broke the integer up into the 3 digit groupings and parsed those groupings on a smaller scale it would be more foolproof.

Here is an alternative for you.

C#
public static string LongToNumeral(this long number)
{
    // check negative
    bool neg = number < 0;
    if (neg) number = Math.Abs(number);

    // function to assist in transformation of groupings
    Func<long, string> Base = i =>
    {
        switch (i)
        {
            case 0: return "zero";
            case 1: return "one";
            case 2: return "two";
            case 3: return "three";
            case 4: return "four";
            case 5: return "five";
            case 6: return "six";
            case 7: return "seven";
            case 8: return "eight";
            case 9: return "nine";
            case 10: return "ten";
            case 11: return "eleven";
            case 12: return "twelve";
            case 13: return "thirteen";
            case 14: return "fourteen";
            case 15: return "fifteen";
            case 16: return "sixteen";
            case 17: return "seventeen";
            case 18: return "eighteen";
            case 19: return "nineteen";
            case 20: return "twenty";
            case 30: return "thirty";
            case 40: return "forty";
            case 50: return "fifty";
            case 60: return "sixty";
            case 70: return "seventy";
            case 80: return "eighty";
            case 90: return "ninety";
            case 100: return "hundred";
        }
        return "";
    };

    // array that lists out magnitudes
    var magnitudes = new string[] {
            "",
            "thousand",
            "million",
            "billion",
            "trillion",
            "quadrillion",
            "quintillion",
            "sextillion",
            "septillion",
            "octillion",
            "nonillion",
            "decillion",
            "undecillion",
            "duodecillion",
            "tredecillion",
            "quattuordecillion",
            "quindecillion",
            "sexdecillion",
            "septendecillion",
            "octodecillion",
            "novemdecillion",
            "vigintillion",
            "X 10^{0}"
        };

    // function that transforms a grouping into its string representation without the magnitude
    Func<int, string> toGroup = num =>
        {
            string ret = string.Empty;

            if (num >= 100)
            {
                int round = num / 100;
                if (num - (round * 100) != 0)
                {
                    ret = Base(round) + " " + Base(100) + " and ";
                    num = num - (round * 100);
                }
                else
                    return Base(round) + " " + Base(100);
            }

            //Over twenty
            if (num > 20)
            {
                int round = num / 10;
                if (num - (round * 10) != 0)
                    return ret + Base(round * 10) + "-" + Base(num - (round * 10));
                else
                    return ret + Base(round * 10);
            }

            return ret + Base(num);
        };

    // function to transform a grouping into its string representation with the magnitude
    Func<int, string, string> toMagnatude = (mag, num) =>
        {
            if (num == "000") return string.Empty;

            return toGroup(int.Parse(num)) + " " + string.Format(
                magnitudes[Math.Min(mag, magnitudes.Length - 1)],
                mag * 3);
        };

    if (number == 0) return "zero";

    // transform number to a string
    var sNum = number.ToString();
    // pad the left of the string to get a multiple of 3
    if (sNum.Length % 3 > 0)
        sNum = sNum.PadLeft((3 - sNum.Length % 3) + sNum.Length, '0');

    // split string into groups of 3 digits
    var groups = new List<string>();
    for (int i = 0; i < sNum.Length; i = i + 3)
        groups.Add(sNum.Substring(i, 3));

    // reverse the groupings so that we prepend the highest magnitude group last
    // numbers are read left to right but magnitude is tracked right to left.
    groups.Reverse();

    // build the final string
    var final = string.Empty;
    for (int i = 0; i < groups.Count; ++i)
        final = toMagnatude(i, groups[i]) + " " + final;

    // return with consideration for negative
    return (neg ? "minus " : "") + final.Replace("  ", " ").Trim();
}


modified 14-May-12 15:47pm.

AnswerRe: flawed logic? Pin
Mossmyr14-May-12 13:36
Mossmyr14-May-12 13:36 
GeneralRe: flawed logic? Pin
PIEBALDconsult15-May-12 3:25
mvePIEBALDconsult15-May-12 3:25 
GeneralRe: flawed logic? Pin
Mossmyr15-May-12 5:05
Mossmyr15-May-12 5:05 
GeneralRe: flawed logic? Pin
Mossmyr16-May-12 11:45
Mossmyr16-May-12 11:45 
GeneralRe: flawed logic? Pin
PIEBALDconsult16-May-12 13:55
mvePIEBALDconsult16-May-12 13:55 
GeneralRe: flawed logic? Pin
Mossmyr17-May-12 5:37
Mossmyr17-May-12 5:37 
SuggestionRe: flawed logic? Pin
Andreas Gieriet15-May-12 0:19
professionalAndreas Gieriet15-May-12 0:19 
GeneralRe: flawed logic? Pin
Daniel Gidman15-May-12 8:13
professionalDaniel Gidman15-May-12 8:13 
GeneralRe: flawed logic? Pin
Andreas Gieriet15-May-12 10:30
professionalAndreas Gieriet15-May-12 10:30 
GeneralRe: flawed logic? Pin
Selvin16-May-12 4:15
Selvin16-May-12 4:15 
GeneralRe: flawed logic? Pin
Daniel Gidman16-May-12 5:43
professionalDaniel Gidman16-May-12 5:43 
QuestionRe: flawed logic? Pin
Mossmyr17-May-12 5:38
Mossmyr17-May-12 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.