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
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.
Use a conversion function to convert numbers to strings or strings to numbers. Conversion functions can be used with String and Number data types.
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 string
s, enum
s, DateTime
s, TimeSpan
s 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
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With