By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,302 Members | 3,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,302 IT Pros & Developers. It's quick & easy.

Numbers in words

100+
P: 271
hi friends,
i need a small help. can any one help in converting the numbers entered in a text box to words.

thanks in advance.....
Jan 11 '07 #1
Share this Question
Share on Google+
13 Replies


hariharanmca
100+
P: 1,977
hi friends,
i need a small help. can any one help in converting the numbers entered in a text box to words.

thanks in advance.....


'Class ConvertMoney
Private m_19AndUnder(19) As String
Private m_Tens(9) As String
Private m_Hundred As String
Private m_Groups(10) As String
Private m_Dollar As String
Private m_Dollars As String
Private m_NoCents As String
Private m_Cent As String
Private m_Cents As String
Private m_Hyphen As String
Private m_And As String
Private m_InvalidAmount As String


Public Function MonetaryToWords(ByVal Value As Variant) As String
Dim decValue As Double
Dim sValue As String
Dim iDecimal As Integer
Dim sCents As String
Dim sDollars As String
'Convert input into a Decimal value (up to 28 digits)
decValue = CDbl(Value)
If decValue < 0 Then GoTo ER

'Convert the Decimal value back into a string. This eliminates
' any format characters such as "$" or ",".
sValue = CStr(decValue)

'Find the decimal point & extract the dollars from the cents
iDecimal = InStr(1, sValue, ".")
If iDecimal = 0 Then
sDollars = sValue
sCents = "00"
Else
'Extract decimal value
sCents = Mid$(sValue, iDecimal + 1)
If Len(sCents) > 2 Then GoTo ER

'Extract dollars
sDollars = Left$(sValue, iDecimal - 1)

'Fill-out decimal places to two digits
sCents = Left$(sCents & "00", 2)
End If

'At this point,
' sDollars = the whole dollar value (0.. approx 79 Octillion)
' sCents = the cents (00..99)

Debug.Assert (Len(sCents) = 2)
Debug.Assert (Len(sDollars) > 0)
Debug.Assert (Len(sDollars) < 31)

Select Case sCents
Case "00"
sCents = m_NoCents

Case "01"
sCents = sCents & m_Cent

Case Else
sCents = sCents & m_Cents
End Select

MonetaryToWords = DollarsToWords(sDollars) & m_And & sCents

Exit Function

ER:

MonetaryToWords = m_InvalidAmount
End Function



Private Function DollarsToWords(ByVal sDollars As String) As String
Dim sWords As String
Dim decValue As Double
Dim sRemaining As String
Dim s3Digits As String
Dim iGroup As Integer
Dim i100s As Integer
Dim i10s As Integer
Dim i1s As Integer
Dim i99OrLess As Integer
Dim sWork As String

'We had better be passing a valid number
Debug.Assert (IsNumeric(sDollars))

'Check for special cases. This also serves to validate the value
decValue = CDec(sDollars)
Select Case decValue
Case 0
DollarsToWords = m_19AndUnder(decValue) & m_Dollars
Exit Function

Case 1
DollarsToWords = m_19AndUnder(decValue) & m_Dollar
Exit Function

End Select

'There should be no insignificant zeroes, "punctuation" or decimals
Debug.Assert (sDollars = CStr(decValue))

iGroup = 0
sRemaining = sDollars
sWords = ""

'Extract each group of three digits, convert to words and prefix to result
While Len(sRemaining) > 0
iGroup = iGroup + 1

'Extract next group of three digits
If Len(sRemaining) > 3 Then
s3Digits = Right$(sRemaining, 3)
sRemaining = Left$(sRemaining, Len(sRemaining) - 3)
Else
'Fill-out group to three digits
s3Digits = Right$("00" & sRemaining, 3)
sRemaining = ""
End If

Debug.Assert (Len(s3Digits) = 3)

If s3Digits <> "000" Then
i100s = CInt(Left$(s3Digits, 1))
i10s = CInt(Mid$(s3Digits, 2, 1))
i1s = CInt(Right$(s3Digits, 1))
i99OrLess = (i10s * 10) + i1s
sWork = " " & m_Groups(iGroup)

Select Case True
'Do we have 20..99?
Case i10s > 1
Debug.Assert (i10s <= 9)

If i1s > 0 Then
Debug.Assert (i1s <= 9)

sWork = m_Tens(i10s) & m_Hyphen & m_19AndUnder(i1s) & sWork
Else
sWork = m_Tens(i10s) & sWork
End If

'Do we have 01..19?
Case i99OrLess > 0
Debug.Assert (i99OrLess <= 99)

sWork = m_19AndUnder(i99OrLess) & sWork

Case Else
'If we're here, it's because there are no tens or ones
Debug.Assert (i99OrLess = 0)
Debug.Assert (i10s = 0)
Debug.Assert (i1s = 0)
Debug.Assert (Right$(s3Digits, 2) = "00")

'If there's no tens or ones, there better be hundreds
Debug.Assert (i100s > 0)

End Select

If i100s > 0 Then
Debug.Assert (i100s <= 9)

sWork = m_19AndUnder(i100s) & " " & m_Hundred & " " & sWork
End If

Debug.Assert (Len(Trim$(sWork)) > 0)

sWords = sWork & " " & sWords
End If
Wend

DollarsToWords = Trim$(sWords) & m_Dollars
End Function



Private Sub Class_Initialize()
'Initialize all the "words"
m_19AndUnder(0) = "Zero"
m_19AndUnder(1) = "One"
m_19AndUnder(2) = "Two"
m_19AndUnder(3) = "Three"
m_19AndUnder(4) = "Four"
m_19AndUnder(5) = "Five"
m_19AndUnder(6) = "Six"
m_19AndUnder(7) = "Seven"
m_19AndUnder(8) = "Eight"
m_19AndUnder(9) = "Nine"
m_19AndUnder(10) = "Ten"
m_19AndUnder(11) = "Eleven"
m_19AndUnder(12) = "Twelve"
m_19AndUnder(13) = "Thirteen"
m_19AndUnder(14) = "Fourteen"
m_19AndUnder(15) = "Fifteen"
m_19AndUnder(16) = "Sixteen"
m_19AndUnder(17) = "Seventeen"
m_19AndUnder(18) = "Eighteen"
m_19AndUnder(19) = "Nineteen"

m_Tens(2) = "Twenty"
m_Tens(3) = "Thirty"
m_Tens(4) = "Forty"
m_Tens(5) = "Fifty"
m_Tens(6) = "Sixty"
m_Tens(7) = "Seventy"
m_Tens(8) = "Eighty"
m_Tens(9) = "Ninety"

m_Hundred = "Hundred"

m_Groups(1) = ""
m_Groups(2) = "Thousand"
m_Groups(3) = "Million"
m_Groups(4) = "Billion"
m_Groups(5) = "Trillion"
m_Groups(6) = "Quadrillion"
m_Groups(7) = "Quintillion"
m_Groups(8) = "Sextillion"
m_Groups(9) = "Septillion"
m_Groups(10) = "Octillion"

m_Dollar = " Rupee"
m_Dollars = " Rupees"

m_NoCents = "No Paise"
'm_Cent & m_Cents could both be changed to "/100"
m_Cent = " Paise"
m_Cents = " Paise"

'Used for #s like: 23 = "Twenty-Three"
m_Hyphen = "-"

'Used between dollars & cents: "One Dollar and 12 Cents"
m_And = " and "

m_InvalidAmount = "Invalid Rupees Amount."
End Sub
Jan 11 '07 #2

hariharanmca
100+
P: 1,977
'Class ConvertMoney
Private m_19AndUnder(19) As String
Private m_Tens(9) As String
Private m_Hundred As String
Private m_Groups(10) As String
Private m_Dollar As String
Private m_Dollars As String
Private m_NoCents As String
Private m_Cent As String
Private m_Cents As String
Private m_Hyphen As String
Private m_And As String
Private m_InvalidAmount As String


Public Function MonetaryToWords(ByVal Value As Variant) As String
Dim decValue As Double
Dim sValue As String
Dim iDecimal As Integer
Dim sCents As String
Dim sDollars As String
'Convert input into a Decimal value (up to 28 digits)
decValue = CDbl(Value)
If decValue < 0 Then GoTo ER

'Convert the Decimal value back into a string. This eliminates
' any format characters such as "$" or ",".
sValue = CStr(decValue)

'Find the decimal point & extract the dollars from the cents
iDecimal = InStr(1, sValue, ".")
If iDecimal = 0 Then
sDollars = sValue
sCents = "00"
Else
'Extract decimal value
sCents = Mid$(sValue, iDecimal + 1)
If Len(sCents) > 2 Then GoTo ER

'Extract dollars
sDollars = Left$(sValue, iDecimal - 1)

'Fill-out decimal places to two digits
sCents = Left$(sCents & "00", 2)
End If

'At this point,
' sDollars = the whole dollar value (0.. approx 79 Octillion)
' sCents = the cents (00..99)

Debug.Assert (Len(sCents) = 2)
Debug.Assert (Len(sDollars) > 0)
Debug.Assert (Len(sDollars) < 31)

Select Case sCents
Case "00"
sCents = m_NoCents

Case "01"
sCents = sCents & m_Cent

Case Else
sCents = sCents & m_Cents
End Select

MonetaryToWords = DollarsToWords(sDollars) & m_And & sCents

Exit Function

ER:

MonetaryToWords = m_InvalidAmount
End Function



Private Function DollarsToWords(ByVal sDollars As String) As String
Dim sWords As String
Dim decValue As Double
Dim sRemaining As String
Dim s3Digits As String
Dim iGroup As Integer
Dim i100s As Integer
Dim i10s As Integer
Dim i1s As Integer
Dim i99OrLess As Integer
Dim sWork As String

'We had better be passing a valid number
Debug.Assert (IsNumeric(sDollars))

'Check for special cases. This also serves to validate the value
decValue = CDec(sDollars)
Select Case decValue
Case 0
DollarsToWords = m_19AndUnder(decValue) & m_Dollars
Exit Function

Case 1
DollarsToWords = m_19AndUnder(decValue) & m_Dollar
Exit Function

End Select

'There should be no insignificant zeroes, "punctuation" or decimals
Debug.Assert (sDollars = CStr(decValue))

iGroup = 0
sRemaining = sDollars
sWords = ""

'Extract each group of three digits, convert to words and prefix to result
While Len(sRemaining) > 0
iGroup = iGroup + 1

'Extract next group of three digits
If Len(sRemaining) > 3 Then
s3Digits = Right$(sRemaining, 3)
sRemaining = Left$(sRemaining, Len(sRemaining) - 3)
Else
'Fill-out group to three digits
s3Digits = Right$("00" & sRemaining, 3)
sRemaining = ""
End If

Debug.Assert (Len(s3Digits) = 3)

If s3Digits <> "000" Then
i100s = CInt(Left$(s3Digits, 1))
i10s = CInt(Mid$(s3Digits, 2, 1))
i1s = CInt(Right$(s3Digits, 1))
i99OrLess = (i10s * 10) + i1s
sWork = " " & m_Groups(iGroup)

Select Case True
'Do we have 20..99?
Case i10s > 1
Debug.Assert (i10s <= 9)

If i1s > 0 Then
Debug.Assert (i1s <= 9)

sWork = m_Tens(i10s) & m_Hyphen & m_19AndUnder(i1s) & sWork
Else
sWork = m_Tens(i10s) & sWork
End If

'Do we have 01..19?
Case i99OrLess > 0
Debug.Assert (i99OrLess <= 99)

sWork = m_19AndUnder(i99OrLess) & sWork

Case Else
'If we're here, it's because there are no tens or ones
Debug.Assert (i99OrLess = 0)
Debug.Assert (i10s = 0)
Debug.Assert (i1s = 0)
Debug.Assert (Right$(s3Digits, 2) = "00")

'If there's no tens or ones, there better be hundreds
Debug.Assert (i100s > 0)

End Select

If i100s > 0 Then
Debug.Assert (i100s <= 9)

sWork = m_19AndUnder(i100s) & " " & m_Hundred & " " & sWork
End If

Debug.Assert (Len(Trim$(sWork)) > 0)

sWords = sWork & " " & sWords
End If
Wend

DollarsToWords = Trim$(sWords) & m_Dollars
End Function



Private Sub Class_Initialize()
'Initialize all the "words"
m_19AndUnder(0) = "Zero"
m_19AndUnder(1) = "One"
m_19AndUnder(2) = "Two"
m_19AndUnder(3) = "Three"
m_19AndUnder(4) = "Four"
m_19AndUnder(5) = "Five"
m_19AndUnder(6) = "Six"
m_19AndUnder(7) = "Seven"
m_19AndUnder(8) = "Eight"
m_19AndUnder(9) = "Nine"
m_19AndUnder(10) = "Ten"
m_19AndUnder(11) = "Eleven"
m_19AndUnder(12) = "Twelve"
m_19AndUnder(13) = "Thirteen"
m_19AndUnder(14) = "Fourteen"
m_19AndUnder(15) = "Fifteen"
m_19AndUnder(16) = "Sixteen"
m_19AndUnder(17) = "Seventeen"
m_19AndUnder(18) = "Eighteen"
m_19AndUnder(19) = "Nineteen"

m_Tens(2) = "Twenty"
m_Tens(3) = "Thirty"
m_Tens(4) = "Forty"
m_Tens(5) = "Fifty"
m_Tens(6) = "Sixty"
m_Tens(7) = "Seventy"
m_Tens(8) = "Eighty"
m_Tens(9) = "Ninety"

m_Hundred = "Hundred"

m_Groups(1) = ""
m_Groups(2) = "Thousand"
m_Groups(3) = "Million"
m_Groups(4) = "Billion"
m_Groups(5) = "Trillion"
m_Groups(6) = "Quadrillion"
m_Groups(7) = "Quintillion"
m_Groups(8) = "Sextillion"
m_Groups(9) = "Septillion"
m_Groups(10) = "Octillion"

m_Dollar = " Rupee"
m_Dollars = " Rupees"

m_NoCents = "No Paise"
'm_Cent & m_Cents could both be changed to "/100"
m_Cent = " Paise"
m_Cents = " Paise"

'Used for #s like: 23 = "Twenty-Three"
m_Hyphen = "-"

'Used between dollars & cents: "One Dollar and 12 Cents"
m_And = " and "

m_InvalidAmount = "Invalid Rupees Amount."
End Sub


give value to MonetaryToWords argument it will return string
Jan 11 '07 #3

hariharanmca
100+
P: 1,977
give value to MonetaryToWords argument it will return string

It's in VB 6 Code
Jan 11 '07 #4

100+
P: 271
hi can u get me the same coding in c# with a frame work of 1.1
Jan 11 '07 #5

hariharanmca
100+
P: 1,977
hi can u get me the same coding in c# with a frame work of 1.1

i KNOW vb AND vb.Net and some C#. it's okye lets we start a Detail Discussion on that
Jan 11 '07 #6

100+
P: 271
that will be fine,

can u help me in the procedure to do that and i will solve it. i think that will be nice right
Jan 11 '07 #7

Jimmy611
P: 52
there you go K!

-------------------------------------------------------------------------------------------------------------------
public static string ConvertNumberToWord(long nNumber)
{
long CurrentNumber = nNumber;
string sReturn = "";

if(CurrentNumber >= 1000000000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/1000000000, "Billion");
CurrentNumber = CurrentNumber % 1000000000;
}
if(CurrentNumber >= 1000000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/1000000, "Million");
CurrentNumber = CurrentNumber % 1000000;
}
if (CurrentNumber >= 100000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber / 100000, "Lacs");
CurrentNumber = CurrentNumber % 100000;
}
if(CurrentNumber >= 1000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/1000, "Thousand");
CurrentNumber = CurrentNumber % 1000;
}
if(CurrentNumber >= 100)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/100, "Hundred");
CurrentNumber = CurrentNumber % 100;
}
if(CurrentNumber >= 20)
{
sReturn = sReturn + " " + GetWord(CurrentNumber, "");
CurrentNumber = CurrentNumber % 10;
}
else if(CurrentNumber > 0)
{
sReturn = sReturn + " " + GetWord(CurrentNumber, "");
CurrentNumber = 0;
}
return sReturn.Replace(" ", " ").Trim();
}

public static string GetWord(long nNumber, string sPrefix)
{
long nCurrentNumber = nNumber;
string sReturn = "";
while(nCurrentNumber > 0)
{
if(nCurrentNumber > 100)
{
sReturn = sReturn + " " + GetWord(nCurrentNumber / 100, "Hundred");
nCurrentNumber = nCurrentNumber % 100;
}
else if(nCurrentNumber > 20)
{
sReturn = sReturn + " " + GetTwentyWord(nCurrentNumber / 10);
nCurrentNumber = nCurrentNumber % 10;
}
else
{
sReturn = sReturn + " " + GetLessThanTwentyWord(nCurrentNumber);
nCurrentNumber = 0;
}
}
sReturn = sReturn + " " + sPrefix;
return sReturn;
}

public static string GetTwentyWord(long nNumber)
{
string sReturn = "";
switch(nNumber)
{
case 2:
sReturn = "Twenty";
break;
case 3:
sReturn = "Thirty";
break;
case 4:
sReturn = "Forty";
break;
case 5:
sReturn = "Fifty";
break;
case 6:
sReturn = "Sixty";
break;
case 7:
sReturn = "Seventy";
break;
case 8:
sReturn = "Eighty";
break;
case 9:
sReturn = "Ninety";
break;
}
return sReturn;
}

public static string GetLessThanTwentyWord(long nNumber)
{
string sReturn = "";
switch(nNumber)
{
case 1:
sReturn = "One";
break;
case 2:
sReturn = "Two";
break;
case 3:
sReturn = "Three";
break;
case 4:
sReturn = "Four";
break;
case 5:
sReturn = "Five";
break;
case 6:
sReturn = "Six";
break;
case 7:
sReturn = "Seven";
break;
case 8:
sReturn = "Eight";
break;
case 9:
sReturn = "Nine";
break;
case 10:
sReturn = "Ten";
break;
case 11:
sReturn = "Eleven";
break;
case 12:
sReturn = "Twelve";
break;
case 13:
sReturn = "Thirteen";
break;
case 14:
sReturn = "Forteen";
break;
case 15:
sReturn = "Fifteen";
break;
case 16:
sReturn = "Sixteen";
break;
case 17:
sReturn = "Seventeen";
break;
case 18:
sReturn = "Eighteen";
break;
case 19:
sReturn = "Nineteen";
break;
}
return sReturn;
}
------------------------------------------------------------------------------------------------------------------
Now to use it you need to pass the number in long and it will return a string.
String str = ConvertNoToWords.ConvertNumberToWords(Number)

ConvertNoToWords = Class containing ConvertNumberToWords
Number = Long Number from Textbox.
------------------------------------------------------------------------------------------------------------------
I hope i am clear and it helps you out.
Jan 11 '07 #8

hariharanmca
100+
P: 1,977
there you go K!

-------------------------------------------------------------------------------------------------------------------
public static string ConvertNumberToWord(long nNumber)
{
long CurrentNumber = nNumber;
string sReturn = "";

if(CurrentNumber >= 1000000000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/1000000000, "Billion");
CurrentNumber = CurrentNumber % 1000000000;
}
if(CurrentNumber >= 1000000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/1000000, "Million");
CurrentNumber = CurrentNumber % 1000000;
}
if (CurrentNumber >= 100000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber / 100000, "Lacs");
CurrentNumber = CurrentNumber % 100000;
}
if(CurrentNumber >= 1000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/1000, "Thousand");
CurrentNumber = CurrentNumber % 1000;
}
if(CurrentNumber >= 100)
{
sReturn = sReturn + " " + GetWord(CurrentNumber/100, "Hundred");
CurrentNumber = CurrentNumber % 100;
}
if(CurrentNumber >= 20)
{
sReturn = sReturn + " " + GetWord(CurrentNumber, "");
CurrentNumber = CurrentNumber % 10;
}
else if(CurrentNumber > 0)
{
sReturn = sReturn + " " + GetWord(CurrentNumber, "");
CurrentNumber = 0;
}
return sReturn.Replace(" ", " ").Trim();
}

public static string GetWord(long nNumber, string sPrefix)
{
long nCurrentNumber = nNumber;
string sReturn = "";
while(nCurrentNumber > 0)
{
if(nCurrentNumber > 100)
{
sReturn = sReturn + " " + GetWord(nCurrentNumber / 100, "Hundred");
nCurrentNumber = nCurrentNumber % 100;
}
else if(nCurrentNumber > 20)
{
sReturn = sReturn + " " + GetTwentyWord(nCurrentNumber / 10);
nCurrentNumber = nCurrentNumber % 10;
}
else
{
sReturn = sReturn + " " + GetLessThanTwentyWord(nCurrentNumber);
nCurrentNumber = 0;
}
}
sReturn = sReturn + " " + sPrefix;
return sReturn;
}

public static string GetTwentyWord(long nNumber)
{
string sReturn = "";
switch(nNumber)
{
case 2:
sReturn = "Twenty";
break;
case 3:
sReturn = "Thirty";
break;
case 4:
sReturn = "Forty";
break;
case 5:
sReturn = "Fifty";
break;
case 6:
sReturn = "Sixty";
break;
case 7:
sReturn = "Seventy";
break;
case 8:
sReturn = "Eighty";
break;
case 9:
sReturn = "Ninety";
break;
}
return sReturn;
}

public static string GetLessThanTwentyWord(long nNumber)
{
string sReturn = "";
switch(nNumber)
{
case 1:
sReturn = "One";
break;
case 2:
sReturn = "Two";
break;
case 3:
sReturn = "Three";
break;
case 4:
sReturn = "Four";
break;
case 5:
sReturn = "Five";
break;
case 6:
sReturn = "Six";
break;
case 7:
sReturn = "Seven";
break;
case 8:
sReturn = "Eight";
break;
case 9:
sReturn = "Nine";
break;
case 10:
sReturn = "Ten";
break;
case 11:
sReturn = "Eleven";
break;
case 12:
sReturn = "Twelve";
break;
case 13:
sReturn = "Thirteen";
break;
case 14:
sReturn = "Forteen";
break;
case 15:
sReturn = "Fifteen";
break;
case 16:
sReturn = "Sixteen";
break;
case 17:
sReturn = "Seventeen";
break;
case 18:
sReturn = "Eighteen";
break;
case 19:
sReturn = "Nineteen";
break;
}
return sReturn;
}
------------------------------------------------------------------------------------------------------------------
Now to use it you need to pass the number in long and it will return a string.
String str = ConvertNoToWords.ConvertNumberToWords(Number)

ConvertNoToWords = Class containing ConvertNumberToWords
Number = Long Number from Textbox.
------------------------------------------------------------------------------------------------------------------
I hope i am clear and it helps you out.

Then what about Decimals
Jan 11 '07 #9

Jimmy611
P: 52
Is it a conversion for money in number to words ?
Jan 11 '07 #10

Jimmy611
P: 52
Ok i suppose it is.
So for the Paise conversion to words we can do it by searching for the decimal(.) or comma(,) and repeating the Tens words.

Correct me if i am wrong.
Jan 11 '07 #11

hariharanmca
100+
P: 1,977
Ok i suppose it is.
So for the Paise conversion to words we can do it by searching for the decimal(.) or comma(,) and repeating the Tens words.

Correct me if i am wrong.
yes, We have to convert Paise also, and you are in the Rightway
Jan 11 '07 #12

Jimmy611
P: 52
O.K. . Then i am sure K! know what he should do now. :)
Jan 11 '07 #13

hariharanmca
100+
P: 1,977
O.K. . Then i am sure K! know what he should do now. :)

Nothing to do, you gave the C# Coding then he can just use it
OK
Jan 11 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.