I am using the following function to convert numbers to text:
public static string NumberToText(long number)
{
StringBuilder wordNumber = new StringBuilder();
string[] powers = new string[] { "Thousand ", "Million ", "Billion " };
string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
string[] ones = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
if (number == 0) { return "Zero"; }
if (number < 0)
{
wordNumber.Append("Negative ");
number = -number;
}
long[] groupedNumber = new long[] { 0, 0, 0, 0 };
int groupIndex = 0;
while (number > 0)
{
groupedNumber[groupIndex++] = number % 1000;
number /= 1000;
}
for (int i = 3; i >= 0; i--)
{
long group = groupedNumber[i];
if (group >= 100)
{
wordNumber.Append(ones[group / 100 - 1] + " Hundred ");
group %= 100;
if (group == 0 && i > 0)
wordNumber.Append(powers[i - 1]);
}
if (group >= 20)
{
if ((group % 10) != 0)
wordNumber.Append(tens[group / 10 - 2] + " " + ones[group % 10 - 1] + " ");
else
wordNumber.Append(tens[group / 10 - 2] + " ");
}
else if (group > 0)
wordNumber.Append(ones[group - 1] + " ");
if (group != 0 && i > 0)
wordNumber.Append(powers[i - 1]);
}
return wordNumber.ToString().Trim();
}
This works fine.
The issue is that the returned text is too long to read. I want to convert it to something shorter.
For eg: 345435234 is returned as three hundred and forty-five million, four hundred and thirty-five thousand, two hundred and thirty-four.
I instead would like 345.4 Mil.
Here are some more examples:
3454 should be 3.4K
34543 should be 34.5K
345433 should be 345.4K
... and so on.
How do I achieve this?
Maybe something like this?
Though if you have "printable character OCD" you could probably get it smaller
private static string[] suffixes = new[] { "", "K", "Mill", "Bill", "Trill", "Zill" };
public static string ToStuff(double number, int precision = 2)
{
const double unit = 1000;
var i = 0;
while (number > unit)
{
number /= unit;
i++;
}
if(i >= 5) throw new Exception("No one can count this high");
return Math.Round(number, precision, MidpointRounding.AwayFromZero) + suffixes[i];
}
Disclaimer : Totally untested
Update
Due to popular demand, i tested it with a test case of 1 E.g 2500000, 0 and it output 2Mill. I think i might have a career in this counting game
Update 2
Due to more popular demand, the consensus is that we shouldn't use .Net default rounding ToEven (bankers rounding) , and should use AwayFromZero
Try the following:
if (number>1000000)
string = floor(number/1000000).ToString() + "m";
else if (number > 1000)
string = floor(number/1000).ToString() + "k";
else
string = number.ToString();
Related
I need a help because I've created a program wherein if the user inputs enter an integer number then it will convert the number into Roman Numerals and in Words. But here in my codes, it's only converting it into roman numerals. How can I include literal words of the user input integer into words below the roman numeral.
For example:
Enter a number: 1
Roman Numeral: I
Number in Words: One
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab_Activity_2
{
internal class Program
{
static void Main(string[] args)
{
string[] units = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
string[] decs = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
string[] cents = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
string[] mills = { "", "M", "MM", "MMM" };
Console.WriteLine("Enter a number: ");
int n = int.Parse(Console.ReadLine());
if (!(n >= 1 && n <= 3999))
{
Console.WriteLine("Number not valid!");
return;
}
int m = n / 1000;
int mig = n % 1000;
int c = mig / 100;
mig = mig % 100;
int d = mig / 10;
mig = mig % 10;
int u = mig;
Console.WriteLine(mills.ElementAtOrDefault(m) + cents.ElementAtOrDefault(c) + decs.ElementAtOrDefault(d) + units.ElementAtOrDefault(u));
}
}
}
I tried to convert the numbers into roman numerals but I don't know how to code the literal words of an integer and where to put it.
Try following IntToRoman() and RomanToInt() APIs:
You can also find other solution via leetcode discussion tab :
https://leetcode.com/problems/integer-to-roman/
https://leetcode.com/problems/roman-to-integer/
/// 12. Integer to Roman
/// Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
/// I 1
/// V 5
/// X 10
/// L 50
/// C 100
/// D 500
/// M 1000
/// CM 900
/// CD 400
/// XC 90
/// XL 40
/// IX 9
/// IV 4
public string IntToRoman(int num)
{
if (num >= 1000) return "M" + IntToRoman(num - 1000);
if (num >= 900) return "CM" + IntToRoman(num - 900);
if (num >= 500) return "D" + IntToRoman(num - 500);
if (num >= 400) return "CD" + IntToRoman(num - 400);
if (num >= 100) return "C" + IntToRoman(num - 100);
if (num >= 90) return "XC" + IntToRoman(num - 90);
if (num >= 50) return "L" + IntToRoman(num - 50);
if (num >= 40) return "XL" + IntToRoman(num - 40);
if (num >= 10) return "X" + IntToRoman(num - 10);
if (num >= 9) return "IX" + IntToRoman(num - 9);
if (num >= 5) return "V" + IntToRoman(num - 5);
if (num >= 4) return "IV" + IntToRoman(num - 4);
if (num > 1) return "I" + IntToRoman(num - 1);
if (num == 1) return "I";
return string.Empty;
}
///13. Roman to Integer
///Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
///It is guaranteed that s is a valid roman numeral in the range [1, 3999]
public int RomanToInt(string s)
{
Dictionary<char, int> dictionary = new Dictionary<char, int>()
{
{ 'I', 1},
{ 'V', 5},
{ 'X', 10},
{ 'L', 50},
{ 'C', 100},
{ 'D', 500},
{ 'M', 1000}
};
int number = 0;
for (int i = 0; i < s.Length; i++)
{
if (i + 1 < s.Length && dictionary[s[i]] < dictionary[s[i + 1]])
{
number -= dictionary[s[i]];
}
else
{
number += dictionary[s[i]];
}
}
return number;
}
I am following a tutorial regarding converting an integer number into a spoken-word equivalent in C#.
I am getting a bit confused about the three digit rule.
// Single-digit and small number names
private static string[] smallNumbers = new string[]
{
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"
};
// Tens number names from twenty upwards
private static string[] tens = new string[]
{
"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"
};
// Scale number names for use during recombination
private static string[] scaleNumbers = new string[]
{
"", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion"
};
public static string ToWords(this BigInteger number)
{
if (number == 0)
{
return "zero";
}
int[] digitGroups = new int[groups];
// Ensure a positive number to extract from
var positive = BigInteger.Abs(number);
// Extract the three-digit groups
for (int i = 0; i < groups; i++)
{
digitGroups[i] = (int)(positive % 1000);
positive /= 1000;
}
//The rest of the code..
}
I am assuming now we are converting a number and its value is 25111.
In the for-loop function, the return value of (int)(positive % 1000) should be 111. The 111 does not match any elements in digitalGroups array.
I don't quite get it, can someone explain it to me? Thanks in advance.
The code you are showing us is not matching but rather assigning the value 111 to the first item of the digitGroupsArray.
How many items has digitGroupsArray? I don't know, it depends on the 'groups' variable value, which we can't see in the code excerpt.
Here:
int[] digitGroups = new int[groups];
you're creating a new empty integer array called digitGroups with the length of (int) 'group' value.
While this,
for (int i = 0; i < groups; i++)
{
digitGroups[i] = (int)(positive % 1000);
positive /= 1000;
}
is a cylce, an iteration. And note that each time the 'positive' variable gets divided by 1000 (like positive = positive / 1000).
The result will be like this:
digitGroups[0] = (int)(25111 % 1000) // first item of digitGroupsarray
'positive' gets divided (25111 / 1000) next time will be 25
digitGroups[1] = (int)(25 % 1000) // second item of digitGroupsarray
'positive' gets divided again (25 / 1000) next time will be 0
and so on...
In these situation is very common and useful to log the values and debug the cycle. It really clears up your mind.
Working example:
static void Main(string[] args)
{
int groups = 3;
int[] digitGroups = new int[groups];
int positive = 25111;
for (int x = 0; x < groups; x++)
{
Console.WriteLine("positive value is: " + positive);
digitGroups[x] = (int)(positive % 1000);
positive /= 1000;
Console.WriteLine("item number (index): " + x + " value: " + digitGroups[x]);
}
}
Outputs:
positive value is: 25111
item number (index): 0 value: 111
positive value is: 25
item number (index): 1 value: 25
positive value is: 0
item number (index): 2 value: 0
First I've to say that's I'm not asking, how to convert integer value to the string phrase.
My question is how to get each number exist between words in string as separate value to replace 46 000 000 with forty-six million and 70 000 000 with seventy million. To make it easier and not confuse for example if string value already exists:
string val1 = "replace1";
string val2 = "replace2";
and input string:
string inputStr = "distance from the Sun ranging from 46 000 000 to 70 000 000 km";
to get this result:
distance from the Sun ranging from replace1 to replace2 km
I'm not asking how to replace value to other value, my question is how to extract exist numbers from string as separate values to replace it after.
string foundNum1 = "46 000 000";
string foundNum2 = "70 000 000";
Only then I can use replace method:
string f1 = inputStr.Replace(foundNum1, val1);
string outStr = f1.Replace(foundNum2, val2);
Here is a complete answer for any number you need to convert:
public static string NumberToWords(int number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += NumberToWords(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += NumberToWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += NumberToWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "and ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}
public string ConvertStringNumbersToWrittenForm(string inputStr)
{
var regex = new Regex(#"[\d][\d ]+[\d]");
var matches = regex.Matches(inputStr);
string newString = inputStr;
foreach (var match in matches)
{
string matchStr = match.ToString();
//eliminate spaces
matchStr = matchStr.Replace(" ", string.Empty);
newString = newString.Replace(match.ToString(), NumberToWords(Convert.ToInt32(matchStr)));
}
return newString;
}
To use it, you call:
string wordString = ConvertStringNumbersToWrittenForm("distance from the Sun ranging from 46 000 000 to 70 000 000 km");
and wordString will be the resulting string, with written words for the numbers.
To do this dynamically, you could use a regex like follows:
string val1 = "replace1";
string val2 = "replace2";
string inputStr = "distance from the Sun ranging from 46 000 000 to 70 000 000 km";
var regex = new Regex(#"[\d][\d ]+[\d]");
var matches = regex.Matches(inputStr);
inputStr.Replace(matches[0], val1);
inputStr.Replace(matches[1], val2);
Just note the following:
if there are not two matches, this won't wokr
if the matches are the same, this will always replace both
there are probably other edge cases
This will get you back any group of numbers.
string inputStr = "distance from the Sun ranging from 46 000 000 to 70 000 000 km";
Regex pattern = new Regex(#"([0-9]{1,3}(?:\s[0-9]{3})*)");
foreach (Match match in pattern.Matches(inputStr))
{
Console.WriteLine(match.Groups[1]);
}
This displays each match. Now you would just need to do your replace the way you want. Not sure what you are planning there; that's why I didn't put anything in place.
You can do something like this:
string res = inputStr.Replace("46 000 000", val1).Replace("70 000 000", val2);
I'm pretty sure you do it like this:
inputStr = inputStr.Replace("46 000 000", "forty six million");
inputStr = inputStr.Replace("70 000 000 ", "seventy million");
I have a C# method that converts integer numbers into their English word counterparts; however, how can I implement this so it'll work with a string.
For example, if I have the following string:
There was a dog. The dog ate 1000 bones. After eating, he was very sleepy. He slept for 12 hours.
I want it to parse it and return:
There was a dog. The dog ate one thousand bones. After eating, he was very sleepy. He slept for twelve hours.
How would I go by taking the numbers out of the sentence, and using the conversion method (below)?
public static string NumberToWords(int number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += NumberToWords(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += NumberToWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += NumberToWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "and ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}
I tried doing a foreach, like this, but it didn't work...
var matches = Regex.Matches(myString, "[0-9]+");
foreach(var match in matches)
{
NumberToWords(match);
}
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.
You could use Selman22's approach, but in case you are not using a version of .NET with LINQ, you could use the RegularExpression replace function instead of Match.
http://msdn.microsoft.com/en-us/library/cft8645c(v=vs.110).aspx
You would just need a MatchEvaluator delegate like this:
static string matchEvaluator(Match m)
{
return NumberToWords(int.Parse(m.ToString()));
}
Much more simple way is to use Regex.Replace method utilizing MatchEvaluator.
string input = "There was a dog. The dog ate 1000 bones. After eating, he was very sleepy. He slept for 12 hours.";
string output = Regex.Replace(input, #"\d+", m => NumberToWords(int.Parse(m.Value)));
Minimal Changes to your existing code:
string s = "There was a dog. The dog ate 1000 bones. After eating, he was very sleepy. He slept for 12 hours.";
foreach (var match in Regex.Matches(s, "[0-9]+"))
{
string number = match.ToString();
s = s.Replace(number, NumberToWords(int.Parse(number)));
}
Console.WriteLine(s);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I convert an integer into its verbal representation?
Can anybody give me a primer code I could work on in converting numbers into words?
Converting numbers to words (ranging from -1000 to +1000)
example: 1000 --> one thousand
public static string NumberToWords(int number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += NumberToWords(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += NumberToWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += NumberToWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "and ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}
When I had to solve this problem, I created a hard-coded data dictionary to map between numbers and their associated words. For example, the following might represent a few entries in the dictionary:
{1, "one"}
{2, "two"}
{30, "thirty"}
You really only need to worry about mapping numbers in the 10^0 (1,2,3, etc.) and 10^1 (10,20,30) positions because once you get to 100, you simply have to know when to use words like hundred, thousand, million, etc. in combination with your map. For example, when you have a number like 3,240,123, you get: three million two hundred forty thousand one hundred twenty three.
After you build your map, you need to work through each digit in your number and figure out the appropriate nomenclature to go with it.