This article explains about how to convert a numeric value to words.
For Example: Numeric: 500
In Words: Five hundred Only
Write a Code:
staticclass NumberToWord
{
private static string[] _ones =
{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
};
private static string[] _teens =
{
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
private static string[] _tens =
{
"",
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
private static string[] _thousands =
{
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion"
};
public static string Convert(decimal value)
{
string digits, temp;
bool showThousands = false;
bool allZeros = true;
StringBuilder builder = new StringBuilder();
digits = ((long)value).ToString();
for(int i = digits.Length-1; i >= 0; i--)
{
int ndigit = (int)(digits[i] - '0');
int column = (digits.Length - (i + 1));
switch (column % 3)
{
case 0:
showThousands = true;
if (i == 0)
{
temp =String.Format("{0} ", _ones[ndigit]);
}
else if (digits[i - 1] == '1')
{
temp = String.Format("{0} ", _teens[ndigit]);
i--;
}
else if (ndigit != 0)
{
temp = String.Format("{0} ", _ones[ndigit]);
}
else
{
temp = String.Empty;
if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
showThousands = true;
else
showThousands = false;
}
if (showThousands)
{
if (column > 0)
{
temp = String.Format("{0}{1}{2}",
temp,_thousands[column / 3], allZeros ? " " : " ");
}
allZeros = false;
}
builder.Insert(0, temp);
break;
case 1:
if (ndigit > 0)
{
temp = String.Format("{0}{1}",_tens[ndigit],
(digits[i + 1] != '0') ? "-" : " ");builder.Insert(0, temp);
}
break;
case 2:
if (ndigit > 0)
{
temp = String.Format("{0} hundred ", _ones[ndigit]);
builder.Insert(0, temp);
}
break;
}}
builder.AppendFormat("only", (value - (long)value) * 100);
return String.Format("{0}{1}",
Char.ToUpper(builder[0]),
builder.ToString(1, builder.Length - 1));
}
}
Add following code on Button_Click event
decimal number;
if (!string.IsNullOrEmpty(txtAmount.Text) && decimal.TryParse(txtAmount.Text.Trim(), out number))
{
lblAmountChar.Text = "(" + NumberToWord.Convert(number) + ")";
}
It convert given number to words. Thanks.
For Example: Numeric: 500
In Words: Five hundred Only
Write a Code:
staticclass NumberToWord
{
private static string[] _ones =
{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
};
private static string[] _teens =
{
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
private static string[] _tens =
{
"",
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
private static string[] _thousands =
{
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion"
};
public static string Convert(decimal value)
{
string digits, temp;
bool showThousands = false;
bool allZeros = true;
StringBuilder builder = new StringBuilder();
digits = ((long)value).ToString();
for(int i = digits.Length-1; i >= 0; i--)
{
int ndigit = (int)(digits[i] - '0');
int column = (digits.Length - (i + 1));
switch (column % 3)
{
case 0:
showThousands = true;
if (i == 0)
{
temp =String.Format("{0} ", _ones[ndigit]);
}
else if (digits[i - 1] == '1')
{
temp = String.Format("{0} ", _teens[ndigit]);
i--;
}
else if (ndigit != 0)
{
temp = String.Format("{0} ", _ones[ndigit]);
}
else
{
temp = String.Empty;
if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
showThousands = true;
else
showThousands = false;
}
if (showThousands)
{
if (column > 0)
{
temp = String.Format("{0}{1}{2}",
temp,_thousands[column / 3], allZeros ? " " : " ");
}
allZeros = false;
}
builder.Insert(0, temp);
break;
case 1:
if (ndigit > 0)
{
temp = String.Format("{0}{1}",_tens[ndigit],
(digits[i + 1] != '0') ? "-" : " ");builder.Insert(0, temp);
}
break;
case 2:
if (ndigit > 0)
{
temp = String.Format("{0} hundred ", _ones[ndigit]);
builder.Insert(0, temp);
}
break;
}}
builder.AppendFormat("only", (value - (long)value) * 100);
return String.Format("{0}{1}",
Char.ToUpper(builder[0]),
builder.ToString(1, builder.Length - 1));
}
}
Add following code on Button_Click event
decimal number;
if (!string.IsNullOrEmpty(txtAmount.Text) && decimal.TryParse(txtAmount.Text.Trim(), out number))
{
lblAmountChar.Text = "(" + NumberToWord.Convert(number) + ")";
}
It convert given number to words. Thanks.
Convert number to words
ReplyDeleteConvert Roman to Decimal Number
Convert Decimal To Roman Number
Great Article
DeleteIEEE Projects for Engineering Students
Final Year Projects for CSE
This is really very interesting about converting numbers to words.
ReplyDeleteConvert ASP to ASP.Net
nice post
Deletenice
Deletegood
Deletegood
DeleteThis comment has been removed by the author.
ReplyDeletegood
Deletethanks for sharing this information
ReplyDeletebest python training in chennai
best python training in sholinganallur
best python training institute in omr
python training in omr
selenium training in chennai
best hadoop training in chennai
best devops training in chennai
best web design company in gurgaon
ReplyDeletebest website design in gurgaon
website design services in gurgaon
website design service in gurgaon
best website designing company in gurgaon
website designing services in gurgaon
web design company in gurgaon
best website designing company in india
top website designing company in india
best web design company in gurgaon
best web designing services in gurgaon
best web design services in gurgaon
website designing in gurgaon
website designing company in gurgaon
website design in gurgaon
graphic designing company in gurgaon
website company in gurgaon
website design company in gurgaon
web design services in gurgaon
best website design company in gurgaon
website company in gurgaon
Website design Company in gurgaon
best website designing services in gurgaon
best web design in gurgaon
website designing company in gurgaon
website development company in gurgaon
web development company in gurgaon
website design company
website designing services
Keep posting articles like this one , good job ,you may be interested in this article: gmail.comma.
ReplyDeleteCanli
ReplyDelete