Sample demo code: Write number in plain english
Posted: Fri Jan 09, 2009 5:54 am
Hi,
I have written a simple demo program that converts numbers to plain english.
I think this is a good demo program of many fundamental programming aspects.
I hope it fits in the sample collection. Feel free to rewrite it nicer!
Regards
Csaba
I have written a simple demo program that converts numbers to plain english.
I think this is a good demo program of many fundamental programming aspects.
I hope it fits in the sample collection. Feel free to rewrite it nicer!
Regards
Csaba
- Code: Select all
"""
NumberInEnglish by Csaba Urbaniczky
Purpose: Convert a digit to an string in plain english
Assumptions: -
Effect: Could be set up to extend the integer class with this functionality
Change history: Rewritten version of a Ruby example from 'Learn to Program'
' v 1.0 2009-01-09 Csaba Urbaniczky: First version
"""
class ConvertNumberToPlainEnglish
def numberInEnglish(numberToTranslate as int) as String is shared
try
if numberToTranslate == 0
return "zero" # obvious special case
numberAsText as String = "" # Here is the the resulting text as the translation is added up
# Take care of negative numbers
if numberToTranslate < 0
numberAsText = "minus "
numberToTranslate = Math.abs(numberToTranslate)
# These are the binding numbers and the translated binding word
bindingNumberLookup = [ 1000000000 , 1000000, 1000, 100]
bindingWordLookup = [ ' billion', ' million', ' thousand', ' hundred',]
reminder = numberToTranslate # This is the reminder of the number that will appear as text to the right of a binding word
# Go through the number for binding words and take care of the left part recursively
for i in bindingNumberLookup.count
numberToTranslate = reminder // bindingNumberLookup[i] # The left part to the binding word/number like " hundred"
reminder = reminder % bindingNumberLookup[i] # The right part of the binding word/number
if numberToTranslate > 0 # Recursion to build up the left part of the number
numberAsText += .numberInEnglish(numberToTranslate) + bindingWordLookup[i]
if reminder > 0
numberAsText += ' '# So we don't write as millionone....
# Reminder is now the right part of the binding word and less than 100 - many spacail cases
# Take care of 11 .. 19 teens specials since we can't write 'teenty-two' instead of twelve
if reminder >= 11 and reminder <= 19
# Array used to look up english words for numbers, Since array is zero-based, fill "0:th place" with dummy value
teenagersLookup = ['zero_never_used', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
return numberAsText + teenagersLookup[reminder - 10] #Finished - return with this special case
# Now 0 .. 10, 20 .. 99 in left to translate to plain english
numberToTranslate = reminder // 10 # How many tens left, 0 or >=2
reminder = reminder % 10 # Only single digits left now in reminder
if numberToTranslate > 0 # The tens left
tensPlaceLookup = ['zero_never_used', 'ten', 'twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
numberAsText += tensPlaceLookup[numberToTranslate]
if reminder > 0
numberAsText += "-" # so we don't write 'sixtyfour'
# Take care of 0 .. 9 that is left to check
if reminder > 0 # add string if it is larger than zero
onesPlaceLookup = ['zero_never_used', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'onesPlaceLookupbug']
return numberAsText + onesPlaceLookup[reminder]
else
return numberAsText
catch e as Exception
print e
return numberAsText # end numberInEnglish
def main is shared # called 'static' in error message
try
testNumber= 0
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= 5
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= 15
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= -10
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= 42
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= 492
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= -1492
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= 1341492
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
testNumber= 1341492123
print '[testNumber] is in english: [.numberInEnglish(testNumber)]'
catch e as Exception
print e
# End Class