|
-
May 8th, 2011, 07:15 PM
#1
Thread Starter
Stack Overflow moderator
Number to text
This is my first cool project in a while Everybody always makes numbers-to-text functions some time, but this one goes up to sextillions (and could handle much more than that), has proper grammar, and does irreductible fractions for you!
Code:
Public Function ToWords(ByVal num As Decimal) As String
If num = 0 Then Return "Zero"
Dim illions() As String = {"m", "b", "tr", "quadr", "quint", "sext"} ', "sept", "oct", "non", "dec", "undec", "dodec"} 'Go farther if you want... but you'll have to change the numerical type.
Dim ties() As String = {"twen", "thir", "four", "fif", "six", "seven", "eight", "nine"}
Dim nums() As String = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}
Dim teens() As String = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
Dim result As New System.Text.StringBuilder()
If num < 0 Then
result.Append("negative ")
num = -num
End If
'***illions first...
For i As Integer = illions.Length - 1 To 0 Step -1
'They always come in threes, starting with 1000000.
Dim d As Decimal = 1000
For x As Integer = 0 To i
d *= 1000
Next
Dim n As Integer = 0
While num >= d
n += 1
num -= d
End While
If n > 0 Then
result.Append(ToWords(n))
result.Append(" "c)
result.Append(illions(i))
result.Append("illion, ")
End If
Next
'Then, thousands
Dim thousands As Integer = 0
While num >= 1000
thousands += 1
num -= 1000
End While
If thousands > 0 Then
result.Append(ToWords(thousands))
result.Append(" thousand, ")
End If
'Hundreds
Dim hundreds As Integer = 0
While num >= 100
hundreds += 1
num -= 100
End While
If hundreds > 0 Then
result.Append(ToWords(hundreds))
result.Append(" hundred ")
End If
'Tens
If num >= 20 Then
result.Append(ties(CInt(Math.Floor(num / 10)) - 2))
result.Append("ty-")
num -= CInt(Math.Floor(num / 10)) * 10
End If
If num < 20 AndAlso num > 10 Then
result.Append(teens(CInt(Math.Floor(num - 10)) - 1))
result.Append(" "c)
num -= 10
End If
'Singles
If Math.Floor(num) > 0 Then
result.Append(nums(CInt(Math.Floor(num)) - 1))
result.Append(" "c)
num -= Math.Floor(num)
End If
'Now, the hard part - fractions.
If num > 0 Then
Dim numerator, denominator As Integer
If ToFraction(num, numerator, denominator) Then
result.Append("and ")
result.Append(ToWords(numerator))
result.Append(" "c)
Dim ths() As String = {"half", "third", "fourth", "fifth"}
Dim thp() As String = {"halves", "thirds", "fourths", "fifths"}
If denominator <= ths.Length + 1 Then
result.Append(If(numerator = 1, ths(denominator - 2), thp(denominator - 2)))
result.Append(" "c)
Else
result.Append(ToWords(denominator).Replace(" "c, "-"c)) 'Fourty-millionths, not fourty millionths.
result.Append("th")
If numerator > 1 Then result.Append("s"c)
result.Append(" "c)
End If
Else
result.Append("point ")
Do
num *= 10
result.Append(ToWords(Math.Floor(num)))
result.Append(" "c)
num -= Math.Floor(num)
Loop While num > 0
End If
End If
'Some final processing
result.Remove(result.Length - 1, 1)
If result(result.Length - 1) = ","c Then result.Remove(result.Length - 1, 1)
'Return the result
ToWords = result.ToString().ToLower()
ToWords = Char.ToUpper(ToWords.Chars(0)) & ToWords.Substring(1)
End Function
Public Function ToFraction(ByVal n As Decimal, ByRef numerator As Integer, ByRef denominator As Integer) As Boolean
Try
'We don't support 0.333333333333 = 1/3, unfortunately. :(
numerator = Integer.Parse(n.ToString().Substring(2))
denominator = CInt(10 ^ (n.ToString().Length - 2))
Dim tN As Integer = numerator
Dim tD As Integer = denominator
Dim nFactors As New List(Of Integer)
Dim dFactors As New List(Of Integer)
For d As Integer = 2 To numerator - 1
While tN Mod d = 0
nFactors.Add(d)
tN \= d
End While
Next
If nFactors.Count = 0 Then nFactors.Add(numerator)
For d As Integer = 2 To denominator - 1
While tD Mod d = 0
dFactors.Add(d)
tD \= d
End While
Next
If dFactors.Count = 0 Then dFactors.Add(denominator)
Dim i As Integer
While i < nFactors.Count
Dim j As Integer = dFactors.IndexOf(nFactors(i))
If j > -1 Then
dFactors.RemoveAt(j)
nFactors.RemoveAt(i)
Else
i += 1
End If
End While
numerator = nFactors.Aggregate(Function(m As Integer, o As Integer) m * o)
denominator = dFactors.Aggregate(Function(m As Integer, o As Integer) m * o)
Return True
Catch
Return False
End Try
End Function
Last edited by minitech; May 9th, 2011 at 02:42 PM.
-
May 9th, 2011, 02:34 AM
#2
Re: Number to text
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Debug.Print(ToWords(1235.212)) '~~~ One thousand, two hundred thirty-five and fifty-three five-hundredths
End Sub

If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
May 9th, 2011, 01:11 PM
#3
Thread Starter
Stack Overflow moderator
Re: Number to text
That's really odd, hold on a second. (The answer should be One thousand, two hundred thirty-five and one hundred six five-hundredths, the fractional part is probably getting a 2 removed somewhere where it shouldn't be.)
Fixing now...
-
May 9th, 2011, 01:38 PM
#4
Banned
Re: Number to text
request:
a procedure to convert a number to a kanji number and or an image of the kanji number
-
May 9th, 2011, 02:38 PM
#5
Thread Starter
Stack Overflow moderator
Re: Number to text
Request denied. Sorry, that completely has nothing to do with this thread. You want image recognition and transliteration. And I don't know kanji.
@akileshbc: Okay, bug fixed - I changed the If statements in ToFraction to While loops, for many same prime factors.
Last edited by minitech; May 9th, 2011 at 02:43 PM.
-
May 9th, 2011, 02:49 PM
#6
Banned
Re: Number to text
to be continued
-
May 9th, 2011, 06:00 PM
#7
Re: Number to text
Pretty good post here minitech. Props.
-
May 9th, 2011, 06:11 PM
#8
Thread Starter
Stack Overflow moderator
Re: Number to text
I just realized another mistake in the fractions; 1/22 will display as "one twenty-twoth" I'll try to have it fixed by tomorrow, sorry! :embarrassed:
-
May 18th, 2011, 05:01 PM
#9
Banned
-
May 18th, 2011, 06:04 PM
#10
Thread Starter
Stack Overflow moderator
Re: Number to text
Hooray. I think that might just qualify as spamming.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|