Question: Create a new console application named NumberToText. Create a public static method named ConvertToText that takes in a ulong parameter. You are to convert any
Create a new console application named NumberToText. Create a public static method named ConvertToText that takes in a ulong parameter. You are to convert any number that is passed into the function to its text representation. This might be harder than it looks. You are welcome to attack this problem however you want. You may loop forwards, you may loop backwards, you may work this as a string, you may work this as a number whatever makes sense to you. You may use multiple variables, List, character arrays, or any collection you would like. I recommend looking into StringBuilder, List, and Arrays. They have built in methods that may prove useful if you want to insert a string at the beginning, or at the end, or reverse a list of strings for example. The largest number passed in will be uLong.MaxValue.
I am struggling to get my code to work for ulong data types. I currently get these errors:
The call is ambiguous between the following methods or properties: 'Math.Abs(decimal)' and 'Math.Abs(float)'
Argument 1: cannot convert from 'string' to 'ulong'
Here is the code:
-----------------------
using System;
using static System.Console;
using System.Text;
using System.Text.RegularExpressions;
using System.Numerics;
namespace NumberToText
{
class Program
{
public static string ConvertToText(ulong number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + ConvertToText(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += ConvertToText(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += ConvertToText(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += ConvertToText(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "and ";
var units = new[]
{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tens = new[]
{ "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += units[number];
else
{
words += tens[number / 10];
if ((number % 10) > 0)
words += "-" + units[number % 10];
}
}
return words;
}
static void Main(string[] args)
{
Write("Enter a number: ");
var number = ReadLine();
ConvertToText(number);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
