Home > Archive > Visual Basic > August 2005 > Convert Number to Word
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Convert Number to Word
|
|
| Phoon Chee Keong 2005-08-29, 3:55 am |
| Dear vb expert, i got a code for convert number to number but it not good as
i want. For example:
1) "One hundred nineteen ringgit and ninety eight cents" change to "Ringgit
one hundred nineteen and ninety eight cents only"
2) "One hundred nineteen ringgit" change to "Ringgit one hundred nineteen
only"
Can anyone help me on this? Thank you
========================================
=================
Public Function numWor(a As String) As String
If IsNumeric(Val(numWor)) = False Then
numWor = ""
Exit Function
End If
Dim conNum(4)
conNum(1) = "Billion "
conNum(2) = "Million "
conNum(3) = "Thousand "
conNum(4) = " "
Dim f As String
Dim s As String
a = Format(a, "000000000000.00")
d = 1
For i = 1 To 4
If Val(Mid(a, d, 3)) > 0 Then
If Val(Mid(a, d, 1)) > 0 Then
numWor = numWor & convert(Val(Mid(a, d, 1))) & "Hundred "
End If
If Val(Mid(a, d + 1, 2)) >= 20 And Val(Mid(a, d + 1, 2)) <= 99
Then
f = Mid(a, d + 1, 1) & "0"
s = Mid(a, d + 1 + 1, 1)
numWor = numWor & convert(f) & convert(s)
End If
If Val(Mid(a, d + 1, 2)) >= 1 And Val(Mid(a, d + 1, 2)) < 20
Then
numWor = numWor & convert(Val(Mid(a, d + 1, 2)))
End If
numWor = numWor & conNum(i)
End If
d = d + 3
Next
If Val(Mid(a, 1, 12)) > 0 Then numWor = numWor & "ringgit "
If Val(Right(a, 2)) > 0 Then
If Val(Mid(a, 1, 12)) > 0 Then numWor = numWor & "And "
If Val(Right(a, 2)) >= 20 And Val(Right(a, 2)) <= 99 Then
f = Mid(a, 14, 1) & "0"
s = Mid(a, 15, 1)
numWor = numWor & convert(f) & convert(s)
End If
If Val(Right(a, 2)) >= 1 And Val(Right(a, 2)) < 20 Then
numWor = numWor & convert(Val(Right(a, 2)))
End If
numWor = numWor & "Cents "
End If
numWor = StrConv(numWor, vbProperCase)
End Function
Public Function convert(a As String) As String
Dim n
n = Val(a)
Select Case n
Case 0: convert = ""
Case 1: convert = "one "
Case 2: convert = "two "
Case 3: convert = "three "
Case 4: convert = "four "
Case 5: convert = "five "
Case 6: convert = "six "
Case 7: convert = "seven "
Case 8: convert = "eight "
Case 9: convert = "nine "
Case 10: convert = "ten "
Case 11: convert = "eleven "
Case 12: convert = "twelve "
Case 13: convert = "thirteen "
Case 14: convert = "fouteen "
Case 15: convert = "fifteen "
Case 16: convert = "sixteen "
Case 17: convert = "seventeen "
Case 18: convert = "eighteen "
Case 19: convert = "nineteen "
Case 20: convert = "twenty "
Case 30: convert = "thirty "
Case 40: convert = "fourty "
Case 50: convert = "fifty "
Case 60: convert = "sixty "
Case 70: convert = "seventy "
Case 80: convert = "eighty "
Case 90: convert = "ninety "
End Select
End Function
========================================
========
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-29, 3:55 am |
| I'm sort of partial to this method of converting numbers to text.<g>
http://vbnet.mvps.org/code/helpers/numbertotext.htm
Rick
"Phoon Chee Keong" <danphoon@streamyx.com> wrote in message
news:%23gTDhnErFHA.3060@TK2MSFTNGP09.phx.gbl...
> Dear vb expert, i got a code for convert number to number but it not
good as
> i want. For example:
>
> 1) "One hundred nineteen ringgit and ninety eight cents" change to
"Ringgit
> one hundred nineteen and ninety eight cents only"
> 2) "One hundred nineteen ringgit" change to "Ringgit one hundred
nineteen
> only"
>
> Can anyone help me on this? Thank you
>
> ========================================
=================
> Public Function numWor(a As String) As String
> If IsNumeric(Val(numWor)) = False Then
> numWor = ""
> Exit Function
> End If
> Dim conNum(4)
> conNum(1) = "Billion "
> conNum(2) = "Million "
> conNum(3) = "Thousand "
> conNum(4) = " "
> Dim f As String
> Dim s As String
> a = Format(a, "000000000000.00")
> d = 1
> For i = 1 To 4
> If Val(Mid(a, d, 3)) > 0 Then
> If Val(Mid(a, d, 1)) > 0 Then
> numWor = numWor & convert(Val(Mid(a, d, 1))) &
"Hundred "
> End If
> If Val(Mid(a, d + 1, 2)) >= 20 And Val(Mid(a, d + 1, 2))
<= 99
> Then
> f = Mid(a, d + 1, 1) & "0"
> s = Mid(a, d + 1 + 1, 1)
> numWor = numWor & convert(f) & convert(s)
> End If
> If Val(Mid(a, d + 1, 2)) >= 1 And Val(Mid(a, d + 1, 2)) <
20
> Then
> numWor = numWor & convert(Val(Mid(a, d + 1, 2)))
> End If
> numWor = numWor & conNum(i)
> End If
> d = d + 3
> Next
> If Val(Mid(a, 1, 12)) > 0 Then numWor = numWor & "ringgit "
>
> If Val(Right(a, 2)) > 0 Then
> If Val(Mid(a, 1, 12)) > 0 Then numWor = numWor & "And "
> If Val(Right(a, 2)) >= 20 And Val(Right(a, 2)) <= 99 Then
> f = Mid(a, 14, 1) & "0"
> s = Mid(a, 15, 1)
> numWor = numWor & convert(f) & convert(s)
> End If
>
> If Val(Right(a, 2)) >= 1 And Val(Right(a, 2)) < 20 Then
> numWor = numWor & convert(Val(Right(a, 2)))
>
> End If
> numWor = numWor & "Cents "
> End If
> numWor = StrConv(numWor, vbProperCase)
> End Function
>
> Public Function convert(a As String) As String
> Dim n
> n = Val(a)
> Select Case n
> Case 0: convert = ""
> Case 1: convert = "one "
> Case 2: convert = "two "
> Case 3: convert = "three "
> Case 4: convert = "four "
> Case 5: convert = "five "
> Case 6: convert = "six "
> Case 7: convert = "seven "
> Case 8: convert = "eight "
> Case 9: convert = "nine "
> Case 10: convert = "ten "
> Case 11: convert = "eleven "
> Case 12: convert = "twelve "
> Case 13: convert = "thirteen "
> Case 14: convert = "fouteen "
> Case 15: convert = "fifteen "
> Case 16: convert = "sixteen "
> Case 17: convert = "seventeen "
> Case 18: convert = "eighteen "
> Case 19: convert = "nineteen "
> Case 20: convert = "twenty "
> Case 30: convert = "thirty "
> Case 40: convert = "fourty "
> Case 50: convert = "fifty "
> Case 60: convert = "sixty "
> Case 70: convert = "seventy "
> Case 80: convert = "eighty "
> Case 90: convert = "ninety "
> End Select
> End Function
> ========================================
========
>
>
| |
| Michael Cole 2005-08-29, 3:55 am |
| Phoon Chee Keong wrote:
> Dear vb expert, i got a code for convert number to number but it not
> good as i want. For example:
>
> 1) "One hundred nineteen ringgit and ninety eight cents" change to
> "Ringgit one hundred nineteen and ninety eight cents only"
> 2) "One hundred nineteen ringgit" change to "Ringgit one hundred
> nineteen only"
>
> Can anyone help me on this? Thank you
I don't really think it would be that difficult.
Add a line at the front to start the string with "Ringit.
See the line that states, --> If Val(Mid(a, 1, 12)) > 0 Then numWor =
numWor & "ringgit " - remove it.
Add a line at the end to add " only" to the string.
You should be able to do this yourself.
--
Regards,
Michael Cole
| |
| Randy Birch 2005-08-29, 3:55 am |
| Hi Rick ...
I updated that page tonight to make it easier to create the demo by adding
code to use control arrays rather than the 25 separate text boxes.
--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/
----------------------------------------------------------------------------
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:OLn$ztErFHA.2500@TK2MSFTNGP10.phx.gbl...
: I'm sort of partial to this method of converting numbers to text.<g>
:
: http://vbnet.mvps.org/code/helpers/numbertotext.htm
:
: Rick
:
:
:
: "Phoon Chee Keong" <danphoon@streamyx.com> wrote in message
: news:%23gTDhnErFHA.3060@TK2MSFTNGP09.phx.gbl...
: > Dear vb expert, i got a code for convert number to number but it not
: good as
: > i want. For example:
: >
: > 1) "One hundred nineteen ringgit and ninety eight cents" change to
: "Ringgit
: > one hundred nineteen and ninety eight cents only"
: > 2) "One hundred nineteen ringgit" change to "Ringgit one hundred
: nineteen
: > only"
: >
: > Can anyone help me on this? Thank you
: >
: > ========================================
=================
: > Public Function numWor(a As String) As String
: > If IsNumeric(Val(numWor)) = False Then
: > numWor = ""
: > Exit Function
: > End If
: > Dim conNum(4)
: > conNum(1) = "Billion "
: > conNum(2) = "Million "
: > conNum(3) = "Thousand "
: > conNum(4) = " "
: > Dim f As String
: > Dim s As String
: > a = Format(a, "000000000000.00")
: > d = 1
: > For i = 1 To 4
: > If Val(Mid(a, d, 3)) > 0 Then
: > If Val(Mid(a, d, 1)) > 0 Then
: > numWor = numWor & convert(Val(Mid(a, d, 1))) &
: "Hundred "
: > End If
: > If Val(Mid(a, d + 1, 2)) >= 20 And Val(Mid(a, d + 1, 2))
: <= 99
: > Then
: > f = Mid(a, d + 1, 1) & "0"
: > s = Mid(a, d + 1 + 1, 1)
: > numWor = numWor & convert(f) & convert(s)
: > End If
: > If Val(Mid(a, d + 1, 2)) >= 1 And Val(Mid(a, d + 1, 2)) <
: 20
: > Then
: > numWor = numWor & convert(Val(Mid(a, d + 1, 2)))
: > End If
: > numWor = numWor & conNum(i)
: > End If
: > d = d + 3
: > Next
: > If Val(Mid(a, 1, 12)) > 0 Then numWor = numWor & "ringgit "
: >
: > If Val(Right(a, 2)) > 0 Then
: > If Val(Mid(a, 1, 12)) > 0 Then numWor = numWor & "And "
: > If Val(Right(a, 2)) >= 20 And Val(Right(a, 2)) <= 99 Then
: > f = Mid(a, 14, 1) & "0"
: > s = Mid(a, 15, 1)
: > numWor = numWor & convert(f) & convert(s)
: > End If
: >
: > If Val(Right(a, 2)) >= 1 And Val(Right(a, 2)) < 20 Then
: > numWor = numWor & convert(Val(Right(a, 2)))
: >
: > End If
: > numWor = numWor & "Cents "
: > End If
: > numWor = StrConv(numWor, vbProperCase)
: > End Function
: >
: > Public Function convert(a As String) As String
: > Dim n
: > n = Val(a)
: > Select Case n
: > Case 0: convert = ""
: > Case 1: convert = "one "
: > Case 2: convert = "two "
: > Case 3: convert = "three "
: > Case 4: convert = "four "
: > Case 5: convert = "five "
: > Case 6: convert = "six "
: > Case 7: convert = "seven "
: > Case 8: convert = "eight "
: > Case 9: convert = "nine "
: > Case 10: convert = "ten "
: > Case 11: convert = "eleven "
: > Case 12: convert = "twelve "
: > Case 13: convert = "thirteen "
: > Case 14: convert = "fouteen "
: > Case 15: convert = "fifteen "
: > Case 16: convert = "sixteen "
: > Case 17: convert = "seventeen "
: > Case 18: convert = "eighteen "
: > Case 19: convert = "nineteen "
: > Case 20: convert = "twenty "
: > Case 30: convert = "thirty "
: > Case 40: convert = "fourty "
: > Case 50: convert = "fifty "
: > Case 60: convert = "sixty "
: > Case 70: convert = "seventy "
: > Case 80: convert = "eighty "
: > Case 90: convert = "ninety "
: > End Select
: > End Function
: > ========================================
========
: >
: >
:
:
|
|
|
|
|