Reputation: 81
Hey i want ot be able to use this code in a form but I am not experianced enough and is wondering how and where to change this code to be able to use it in in a FORM tried to change to public void etc but only get error messages
static String NumWords(double n) //converts double to words
{
string[] numbersArr = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
string[] suffixesArr = new string[] { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
string words = "";
bool tens = false;
if (n < 0)
{
words += "negative ";
n *= -1;
}
int power = (suffixesArr.Length + 1) * 3;
while (power > 3)
{
double pow = Math.Pow(10, power);
if (n >= pow)
{
if (n % pow > 0)
{
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
}
else if (n % pow == 0)
{
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
}
n %= pow;
}
power -= 3;
}
if (n >= 1000)
{
if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " thousand, ";
else words += NumWords(Math.Floor(n / 1000)) + " thousand";
n %= 1000;
}
if (0 <= n && n <= 999)
{
if ((int)n / 100 > 0)
{
words += NumWords(Math.Floor(n / 100)) + " hundred";
n %= 100;
}
if ((int)n / 10 > 1)
{
if (words != "")
words += " ";
words += tensArr[(int)n / 10 - 2];
tens = true;
n %= 10;
}
if (n < 20 && n > 0)
{
if (words != "" && tens == false)
words += " ";
words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
n -= Math.Floor(n);
}
}
return words;
}
Upvotes: 0
Views: 48
Reputation: 159
You can include this functionality on button click envent or can also call this function onchange event of textbox which will be used to enter numberic value.
// COnvertBtn is button id, on click of which function will get call
//But make sure that text box named as txtNumber should contain number
void ConvertBtn_Click(Object sender, EventArgs e)
{
string number = NumWords(Convert.ToDouble(txtNumber.Text));
}
Otherwise you can call function directly from javascript, here you need to make some changes to function as data type of input is going to be text
Regards, Meet
Upvotes: 1