Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an integer into its verbal representation?

Tags:

c#

.net

.net-3.5

Is there a library or a class/function that I can use to convert an integer to it's verbal representation?

Example input:

4,567,788`

Example output:

Four million, Five hundred sixty-seven thousand, seven hundred eighty-eight

like image 765
Malfist Avatar asked Feb 16 '09 19:02

Malfist


People also ask

Which function is used to convert a number to an integer?

The Excel INT function returns the integer part of a decimal number by rounding down to the integer. Note that negative numbers become more negative. For example, while INT(10.8) returns 10, INT(-10.8) returns -11. The Excel ROUND function returns a number rounded to a given number of digits.

Can you convert numbers to strings in formula tool alteryx?

Use a conversion function to convert numbers to strings or strings to numbers. Conversion functions can be used with String and Number data types.


2 Answers

Currently the best, most robust, library for this is definitely Humanizer. It's open sourced and available as a nuget:

Console.WriteLine(4567788.ToWords()); // => four million five hundred and sixty-seven thousand seven hundred and eighty-eight 

It also has a wide range of tools solving the small problems every application has with strings, enums, DateTimes, TimeSpans and so forth, and supports many different languages.

Console.WriteLine(4567788.ToOrdinalWords().Underscore().Hyphenate().ApplyCase(LetterCasing.AllCaps)); // => FOUR-MILLION-FIVE-HUNDRED-AND-SIXTY-SEVEN-THOUSAND-SEVEN-HUNDRED-AND-EIGHTY-EIGHTH 
like image 100
i3arnon Avatar answered Oct 06 '22 00:10

i3arnon


if you use the code found in: converting numbers in to words C# and you need it for decimal numbers, here is how to do it:

public string DecimalToWords(decimal number) {     if (number == 0)         return "zero";      if (number < 0)         return "minus " + DecimalToWords(Math.Abs(number));      string words = "";      int intPortion = (int)number;     decimal fraction = (number - intPortion)*100;     int decPortion = (int)fraction;      words = NumericToWords(intPortion);     if (decPortion > 0)     {         words += " and ";         words += NumericToWords(decPortion);     }     return words; } 
like image 43
Ruben Carreon Avatar answered Oct 06 '22 00:10

Ruben Carreon