Convert Number to words

Posted by Vivekjj under ASP.NET on 11/9/2012 | Points: 10 | Views : 3419 | Status : [Member] | Replies : 5
Hi i have created crystal report and calculated sum of one column, the sum is 2500 .. I need to convert this 2500 to words like "Two Thousand Five Hundred Only ". Tel me the code if any?




Responses

Posted by: Krishnamanohar on: 11/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Vivek
Try This
Pass Your Value Instead of Number
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;


}

Vivekjj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 11/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
see this link
http://www.dotnetfunda.com/forums/thread2898-how-to-convert-amount-in-words-using-crystal-report.aspx

Thanks & Regards
Hari

Vivekjj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 11/9/2012 [Member] Starter | Points: 25

Up
0
Down
@vivek,
i got error identifier expected at
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" };

Thanks & Regards
Hari

Vivekjj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/6/2012 [Member] Bronze | Points: 25

Up
0
Down
Public Class clsnumtoword
Function strReplicate(ByVal str As String, ByVal intD As Integer) As String
'This fucntion padded "0" after the number to evaluate hundred, thousand and on....
'using this function you can replicate any Charactor with given string.
Dim i As Integer
strReplicate = ""
For i = 1 To intD
strReplicate = strReplicate + str
Next
Return strReplicate
End Function
Function AmtInWord(ByVal Num As Decimal) As String
'I have created this function for converting amount in indian rupees (INR).
'You can manipulate as you wish like decimal setting, Doller (any currency) Prefix.

Dim strNum As String
Dim strNumDec As String
Dim StrWord As String
strNum = Num

If InStr(1, strNum, ".") <> 0 Then
strNumDec = Mid(strNum, InStr(1, strNum, ".") + 1)

If Len(strNumDec) = 1 Then
strNumDec = strNumDec + "0"
End If
If Len(strNumDec) > 2 Then
strNumDec = Mid(strNumDec, 1, 2)
End If

strNum = Mid(strNum, 1, InStr(1, strNum, ".") - 1)
StrWord = IIf(CDbl(strNum) = 1, " Rupee ", " Rupees ") + NumToWord(CDbl(strNum)) + IIf(CDbl(strNumDec) > 0, " and Paise" + cWord3(CDbl(strNumDec)), "")
Else
StrWord = IIf(CDbl(strNum) = 1, " Rupee ", " Rupees ") + NumToWord(CDbl(strNum))
End If
AmtInWord = StrWord & " Only"
Return AmtInWord
End Function
Function NumToWord(ByVal Num As Integer) As String
'I divided this function in two part.
'1. Three or less digit number.
'2. more than three digit number.

Dim strNum As String
Dim StrWord As String
strNum = Num

If Len(strNum) <= 3 Then
StrWord = cWord3(CDbl(strNum))
Else
StrWord = cWordG3(CDbl(Mid(strNum, 1, Len(strNum) - 3))) + " " + cWord3(CDbl(Mid(strNum, Len(strNum) - 2)))
End If
NumToWord = StrWord
End Function
Function cWordG3(ByVal Num As Decimal) As String
'2. more than three digit number.
Dim strNum As String = ""
Dim StrWord As String = ""
Dim readNum As String = ""
strNum = Num
If Len(strNum) Mod 2 <> 0 Then
readNum = CDbl(Mid(strNum, 1, 1))
If readNum <> "0" Then
StrWord = retWord(readNum)
readNum = CDbl("1" + strReplicate("0", Len(strNum) - 1) + "000")
StrWord = StrWord + " " + retWord(readNum)
End If
strNum = Mid(strNum, 2)
End If
While Not Len(strNum) = 0
readNum = CDbl(Mid(strNum, 1, 2))
If readNum <> "0" Then
StrWord = StrWord + " " + cWord3(readNum)
readNum = CDbl("1" + strReplicate("0", Len(strNum) - 2) + "000")
StrWord = StrWord + " " + retWord(readNum)
End If
strNum = Mid(strNum, 3)
End While
cWordG3 = StrWord
Return cWordG3
End Function
Function cWord3(ByVal Num As Decimal) As String
'1. Three or less digit number.
Dim strNum As String = ""
Dim StrWord As String = ""
Dim readNum As String = ""
If Num < 0 Then Num = Num * -1
strNum = Num

If Len(strNum) = 3 Then
readNum = CDbl(Mid(strNum, 1, 1))
StrWord = retWord(readNum) + " Hundred"
strNum = Mid(strNum, 2, Len(strNum))
End If

If Len(strNum) <= 2 Then
If CDbl(strNum) >= 0 And CDbl(strNum) <= 20 Then
StrWord = StrWord + " " + retWord(CDbl(strNum))
Else
StrWord = StrWord + " " + retWord(CDbl(Mid(strNum, 1, 1) + "0")) + " " + retWord(CDbl(Mid(strNum, 2, 1)))
End If
End If

strNum = CStr(Num)
cWord3 = StrWord
Return cWord3
End Function

Function retWord(ByVal Num As Decimal) As String
'This two dimensional array store the primary word convertion of number.
retWord = ""
Dim ArrWordList(,) As Object = {{0, ""}, {1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"}, _
{5, "Five"}, {6, "Six"}, {7, "Seven"}, {8, "Eight"}, {9, "Nine"}, _
{10, "Ten"}, {11, "Eleven"}, {12, "Twelve"}, {13, "Thirteen"}, {14, "Fourteen"}, _
{15, "Fifteen"}, {16, "Sixteen"}, {17, "Seventeen"}, {18, "Eighteen"}, {19, "Nineteen"}, _
{20, "Twenty"}, {30, "Thirty"}, {40, "Forty"}, {50, "Fifty"}, {60, "Sixty"}, _
{70, "Seventy"}, {80, "Eighty"}, {90, "Ninety"}, {100, "Hundred"}, {1000, "Thousand"}, _
{100000, "Lakh"}, {10000000, "Crore"}}

Dim i As Integer
For i = 0 To UBound(ArrWordList)
If Num = ArrWordList(i, 0) Then
retWord = ArrWordList(i, 1)
Exit For
End If
Next
Return retWord
End Function
End Class


Thanks
Pavan Kumar
Mark Answer if this fits the need

Vivekjj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/23/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Number_to_word_in_csharp
{
public partial class Form1 : Form
{
int n = 0;
string x = null;


public Form1()
{
InitializeComponent();
}

private void Button1_Click(object sender, EventArgs e)
{
string[] a = {
"One",
"Two",
"Three",
"four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Ninteen"
};

string[] b = {
"Twenty",
"Thirty",
"Fourty",
"Fifty",
"sixty",
"Seventy",
"eighty",
"ninty"
};

x = "";
n = int.Parse(textBox1.Text);
if ((n <= 9999))
{
if ((n > 999 & n <= 9999))
{
x += a[(n / 1000) - 1] + "Thousand";
n = n % 1000;
}
x += " ";
if ((n > 99 & n <= 999))
{
x += a[(n / 100) - 1] + "Hundred";
n = n % 100;
}
x += " ";
if ((n > 19 & n <= 99))
{
x += b[(n / 10) - 2];
n = n % 10;
}
x += " ";
if ((n > 0 & n <= 19))
{
x += a[n - 1];
}

textBox2.Text = x;

}
else
{
textBox2.Text = ("Number is out of range");

}

}
}
}


If this post helps you mark it as answer
Thanks

Vivekjj, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response