"convert numbers to words C#" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "convert numbers to words C#" answered properly. Developers are finding an appropriate answer about convert numbers to words C# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like convert numbers to words C#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on convert numbers to words C#. 

convert numbers to words C#

By Tilted TurtleTilted Turtle on Apr 03, 2021
using System;
class Program
{
    static void Main()
    {
        string input;
        int number;
        bool isValid;
        bool isUK = false;
        Console.WriteLine("\nEnter '0' to quit the program at any time\n");
        while (true)
        {
            Console.Write("\nUse UK numbering y/n : ");
            input = Console.ReadLine();
            if (!(input.ToLower() == "y" || input.ToLower() == "n"))
                Console.WriteLine("\n  Must be 'y' or 'n', please try again\n");
            else
            {
                if (input.ToLower() == "y") isUK = true;
                Console.WriteLine("\n");
                break;
            }
        }
        do
        {
            Console.Write("Enter integer : ");
            input = Console.ReadLine();
            isValid = int.TryParse(input, out number);
            if (!isValid)
                Console.WriteLine("\n  Not an integer, please try again\n");
            else
                Console.WriteLine("\n  {0}\n", NumberToText(number, isUK));
        }
        while (!(isValid && number == 0));
        Console.WriteLine("\nProgram ended");
    }
    public static string NumberToText(int number, bool isUK)
    {
        if (number == 0) return "Zero";
        string and = isUK ? "and " : ""; // deals with UK or US numbering
        if (number == -2147483648) return "Minus Two Billion One Hundred " + and +
        "Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
        "Six Hundred " + and + "Forty Eight";
        int[] num = new int[4];
        int first = 0;
        int u, h, t;
        System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
        if (number < 0)
        {
            sb.Append("Minus ");
            number = -number;
        }
        string[] words0 = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
        string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
        string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
        string[] words3 = { "Thousand ", "Million ", "Billion " };
        num[0] = number % 1000;           // units
        num[1] = number / 1000;
        num[2] = number / 1000000;
        num[1] = num[1] - 1000 * num[2];  // thousands
        num[3] = number / 1000000000;     // billions
        num[2] = num[2] - 1000 * num[3];  // millions
        for (int i = 3; i > 0; i--)
        {
            if (num[i] != 0)
            {
                first = i;
                break;
            }
        }
        for (int i = first; i >= 0; i--)
        {
            if (num[i] == 0) continue;
            u = num[i] % 10;              // ones
            t = num[i] / 10;
            h = num[i] / 100;             // hundreds
            t = t - 10 * h;               // tens
            if (h > 0) sb.Append(words0[h] + "Hundred ");
            if (u > 0 || t > 0)
            {
                if (h > 0 || i < first) sb.Append(and);
                if (t == 0)
                    sb.Append(words0[u]);
                else if (t == 1)
                    sb.Append(words1[u]);
                else
                    sb.Append(words2[t - 2] + words0[u]);
            }
            if (i != 0) sb.Append(words3[i - 1]);
        }
        return sb.ToString().TrimEnd();
    }
}

Source: www.c-sharpcorner.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on convert numbers to words C# can get a collection of related answers to their query. Programmers need to enter their query on convert numbers to words C# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about convert numbers to words C# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of convert numbers to words C# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "convert numbers to words C#". Visit this developer's friendly online web community, CodeProZone, and get your queries like convert numbers to words C# resolved professionally and stay updated to the latest C# updates. 

C# answers related to "convert numbers to words C#"

View All C# queries

C# queries related to "convert numbers to words C#"

convert numbers to words C# C# convert random numbers in textBox to currency c# break file into words c# words return first 20 items of array todays corona numbers c# linq to select even numbers enums as numbers c# 10 random numbers unity formatting binary numbers in c# regex only letters and numbers c# Write N lines with M numbers each that describe the layout of the second layer in the way shown above c# how to check the minimum and maximum of numbers Scientific Numbers C# Numbers Only unity how to find the smallest value out of 2 numbers divide 3 numbers c# covid numbers unity how to convert mouse screen position to world position c# convert Unix time in seconds to datetime convert string array to int C# how to convert int to string unity c# convert int to string in linq query c# convert array to list Unity C# convert string to boolean c# convert datetime to user timezone c# convert uint to int C# stack overflow c# convert string to int console.readline(convert.toint32) c# c# convert address to int Convert C# Class to xml wth xsd.exe c# convert to snake case asp net mvc convert ienumerable to selectlistitem c# convert to absolute value F# convert generic.List to list c# convert queue to list convert relative path to physical path c# c# convert string to enum value how to convert int to char in c# vb.net convert int32 into boolean array stack overflow convert xml string to file c# how to convert c# string to pdf convert text to ssml function convert list of tuples to dictionary c# cannot implicitly convert type 'system.threading.tasks.task string ' to 'string' c# convert word files to plain text c# c# convert excel column index to letter how to convert a key state to a letter in monogame c# convert linq jValue to int convert dictionary to object c# convert table to Csharp class linq convert list to another list c# convert enumb to int array how to convert from hexadecimal to binary in c# convert to int c# c# convert ad objectguid to string convert foreach to linq c# vbnet programatically convert type to db type c# program for convert kg to pound convert arraylist to array int convert Integer arraylist to array java convert arraylist of integers to array primitive Convert string int Linq

Browse Other Code Languages

CodeProZone