I am currently trying to make an application in C# that takes a ulong number input and outputs the verbal representation of the number (i.e. 12 to "twelve", 45238 to "forty five thousand two hundred thirty eight", etc.) and I'd like for it to be able to pass uLong.MaxValue.
I am new to programming and really struggling with the concept. I've tried to implement Lists and StringBuilder earlier because I figured they could be useful, but I could not figure it out. I am trying to adapt the base code so that it works for ulong data types but I get the following errors:
error CS0121: The call is ambiguous between the following methods or properties: 'Math.Abs(decimal)' and 'Math.Abs(float)' error CS1503: Argument 1: cannot convert from 'string' to 'ulong'
Here is the base 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);
}
}
}