Click here to Skip to main content
15,890,123 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.1K   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 
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 
shouldn't be:

C#
//...
new Tuple<long,string>(1036, "one thousand and thirty-six")
//...

...works fascinates me...i can stare it for hours...

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.