How can I convert an integer into its verbal representation?
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;
}
Convert Number into Word in C# window
You need to move the comparison against beginsZero
to enable changing the place
variable (to remove the 'Hundred', 'Thousand' etc.) when there is no digit between 1 and 9. So, like this:
//check for trailing zeros
if (beginsZero)
{
word = " " + word.Trim();//-----"and"---
place = ""; // Remove the current text as it is not required now
}
word = translateWholeNumber(number.Substring(0, pos)) + place +
translateWholeNumber(number.Substring(pos));
Convert numbers in string to English word format
You could use Split
and LINQ
:
var input = "There was a dog. The dog ate 1000 bones. After eating, he was very sleepy. He slept for 12 hours.";
var invalidChars = new [] { ',', ';', '-', '.' };
var words = input.Split(' ')
.Select(x =>
{
if (x.All(char.IsDigit) || x.Trim(invalidChars).All(char.IsDigit))
return NumberToWords(int.Parse(x.Trim(invalidChars)));
return x;
});
var output = string.Join(" ", words);
Btw I assume that NumberToWord
method is working correctly.
converting all numbers including cents to word
This is the Converter that you need. Using methods reduce complexity of your code.
this algorithm convert number to words like this.
Input : "1234567"
Get the 3 last digits. "567"
Convert it to Words. Five Hundred Sixty Seven
Apply its separator. Five Hundred Sixty Seven (There is no separator for 3 last digits)
Repeat: Get the 3 last digits "234"
Convert it to Words. Two Hundred Thirty Four
Apply its separator. Two Hundred Thirty Four Thousand
Append to Resault.Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
Repeat : Get the 3 last digits. "1" (Only one digit left)
Convert it to Words. One
Apply its separator. One Million
Append to Resault.One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
Done.
I have Commented the code hope it helps.
If you didnt understand some parts just ask ill explain.
Also for Decimals we separate them, convert them to words. finally we add add them to result at the end.
private static void Main(string[] args)
{
string input = "123466265.123";
// take decimal part of input. convert it to word. add it at the end of method.
string decimals = "";
if (input.Contains("."))
{
decimals = input.Substring(input.IndexOf(".") + 1);
// remove decimal part from input
input = input.Remove(input.IndexOf("."));
}
// Convert input into words. save it into strWords
string strWords = GetWords(input);
if (decimals.Length > 0)
{
// if there is any decimal part convert it to words and add it to strWords.
strWords += " Point " + GetWords(decimals);
}
Console.WriteLine(strWords);
}
private static string GetWords(string input)
{
// these are seperators for each 3 digit in numbers. you can add more if you want convert beigger numbers.
string[] seperators = { "", " Thousand ", " Million ", " Billion " };
// Counter is indexer for seperators. each 3 digit converted this will count.
int i = 0;
string strWords = "";
while (input.Length > 0)
{
// get the 3 last numbers from input and store it. if there is not 3 numbers just use take it.
string _3digits = input.Length < 3 ? input : input.Substring(input.Length - 3);
// remove the 3 last digits from input. if there is not 3 numbers just remove it.
input = input.Length < 3 ? "" : input.Remove(input.Length - 3);
int no = int.Parse(_3digits);
// Convert 3 digit number into words.
_3digits = GetWord(no);
// apply the seperator.
_3digits += seperators[i];
// since we are getting numbers from right to left then we must append resault to strWords like this.
strWords = _3digits + strWords;
// 3 digits converted. count and go for next 3 digits
i++;
}
return strWords;
}
// your method just to convert 3digit number into words.
private static string GetWord(int no)
{
string[] Ones =
{
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen"
};
string[] Tens = {"Ten", "Twenty", "Thirty", "Fourty", "Fift", "Sixty", "Seventy", "Eighty", "Ninty"};
string word = "";
if (no > 99 && no < 1000)
{
int i = no/100;
word = word + Ones[i - 1] + " Hundred ";
no = no%100;
}
if (no > 19 && no < 100)
{
int i = no/10;
word = word + Tens[i - 1] + " ";
no = no%10;
}
if (no > 0 && no < 20)
{
word = word + Ones[no - 1];
}
return word;
}
Convert integers to written numbers
This should work reasonably well:
public static class HumanFriendlyInteger
{
static string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
static string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
static string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
static string[] thousandsGroups = { "", " Thousand", " Million", " Billion" };
private static string FriendlyInteger(int n, string leftDigits, int thousands)
{
if (n == 0)
{
return leftDigits;
}
string friendlyInt = leftDigits;
if (friendlyInt.Length > 0)
{
friendlyInt += " ";
}
if (n < 10)
{
friendlyInt += ones[n];
}
else if (n < 20)
{
friendlyInt += teens[n - 10];
}
else if (n < 100)
{
friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0);
}
else if (n < 1000)
{
friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + " Hundred"), 0);
}
else
{
friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands+1), 0);
if (n % 1000 == 0)
{
return friendlyInt;
}
}
return friendlyInt + thousandsGroups[thousands];
}
public static string IntegerToWritten(int n)
{
if (n == 0)
{
return "Zero";
}
else if (n < 0)
{
return "Negative " + IntegerToWritten(-n);
}
return FriendlyInteger(n, "", 0);
}
}
(Edited to fix a bug w/ million, billion, etc.)
Related Topics
Fluent and Query Expression - Is There Any Benefit(S) of One Over Other
Entity Framework 5 Updating a Record
How Is Generic Covariance & Contra-Variance Implemented in C# 4.0
Tips For Optimizing C#/.Net Programs
How to Update the Current Line in a C# Windows Console App
Writing Data into CSV File in C#
How to Get an Htmlelement Value Inside Frames/Iframes
Access the Current Httpcontext in ASP.NET Core
How to Get the Color from a Hexadecimal Color Code Using .Net
Color Different Parts of a Richtextbox String
What's the Difference Between the 'Ref' and 'Out' Keywords
Sharing Sessions Across Applications Using the ASP.NET Session State Service
Login Failed For User 'Iis Apppool\Asp.Net V4.0'
Getting Multiple Keys of Specified Value of a Generic Dictionary