i borrowed and modified this code from Ozgrid.com
Option Compare Database
Option Explicit
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
'Dollars = "No Dollars"
Case "One"
'Dollars = "One Dollar"
Case Else
'Dollars = Dollars & " Dollars"
End Select
Select Case Cents
Case ""
'Cents = " and No Cents"
Case "One"
'Cents = " and One Cent"
Case Else
Cents = " and " & Cents & " hundreds"
End Select
SpellNumber = Dollars & Cents
End Function
'*******************************************
' Converts a number from 100-999 into text *
'*******************************************
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
'*********************************************
' Converts a number from 10 to 99 into text. *
'*********************************************
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "Ten"
Case 11: Result = "Eleven"
Case 12: Result = "Twelve"
Case 13: Result = "Thirteen"
Case 14: Result = "Fourteen"
Case 15: Result = "Fifteen"
Case 16: Result = "Sixteen"
Case 17: Result = "Seventeen"
Case 18: Result = "Eighteen"
Case 19: Result = "Nineteen"
Case Else
End Select
Else ' If value between 20-99...
Select Case Val(Left(TensText, 1))
Case 2: Result = "Twenty "
Case 3: Result = "Thirty "
Case 4: Result = "Forty "
Case 5: Result = "Fifty "
Case 6: Result = "Sixty "
Case 7: Result = "Seventy "
Case 8: Result = "Eighty "
Case 9: Result = "Ninety "
Case Else
End Select
Result = Result & GetDigit(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
'*******************************************
' Converts a number from 1 to 9 into text. *
'*******************************************
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function
Answer from Duane Hookom on learn.microsoft.comVideos
Can I convert decimal numbers?
How many numbers can I convert at once?
What kind of numbers can I convert?
i borrowed and modified this code from Ozgrid.com
Option Compare Database
Option Explicit
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
'Dollars = "No Dollars"
Case "One"
'Dollars = "One Dollar"
Case Else
'Dollars = Dollars & " Dollars"
End Select
Select Case Cents
Case ""
'Cents = " and No Cents"
Case "One"
'Cents = " and One Cent"
Case Else
Cents = " and " & Cents & " hundreds"
End Select
SpellNumber = Dollars & Cents
End Function
'*******************************************
' Converts a number from 100-999 into text *
'*******************************************
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
'*********************************************
' Converts a number from 10 to 99 into text. *
'*********************************************
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "Ten"
Case 11: Result = "Eleven"
Case 12: Result = "Twelve"
Case 13: Result = "Thirteen"
Case 14: Result = "Fourteen"
Case 15: Result = "Fifteen"
Case 16: Result = "Sixteen"
Case 17: Result = "Seventeen"
Case 18: Result = "Eighteen"
Case 19: Result = "Nineteen"
Case Else
End Select
Else ' If value between 20-99...
Select Case Val(Left(TensText, 1))
Case 2: Result = "Twenty "
Case 3: Result = "Thirty "
Case 4: Result = "Forty "
Case 5: Result = "Fifty "
Case 6: Result = "Sixty "
Case 7: Result = "Seventy "
Case 8: Result = "Eighty "
Case 9: Result = "Ninety "
Case Else
End Select
Result = Result & GetDigit(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
'*******************************************
' Converts a number from 1 to 9 into text. *
'*******************************************
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function
Why not use the found module and simply remove all references to "DOLLARS" or "CENTS"?
Ok, I think it's time for my own implementation in Windows BATCH script (should work on Windows 2000 or later).
Here is the code:
@echo off
set zero_to_nineteen=Zero One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen
set twenty_to_ninety=ignore ignore Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety
set big_numbers=ignore Thousand Million Billion Trillion Quadrillion Quintillion Sextillion Septillion Octillion Nonillion Decillion Undecillion Duodecillion Tredecillion Quattuordecillion Quindecillion Sexdecillion Septendecillion Octodecillion Novemdecillion Vigintillion
rem 10^0 10^3 10^6 10^9 10^12 10^15 10^18 10^21 10^24 10^27 10^30 10^33 10^36 10^39 10^42 10^45 10^48 10^51 10^54 10^57 10^60 10^63
call :parse_numbers %*
exit /B 0
:parse_numbers
:parse_numbers_loop
if "$%~1" == "$" goto parse_numbers_end
call :parse_number %~1
echo %~1 -^> %parse_number_result%
shift
goto parse_numbers_loop
:parse_numbers_end
exit /B 0
:parse_number
call :get_sign %~1
set number_sign=%get_sign_result%
call :remove_groups %get_sign_result_number%
call :trim_leading_zeros %remove_groups_result%
set number=%trim_leading_zeros_result%
if "$%number%" == "$0" (
set parse_number_result=Zero
exit /B 0
)
set counter=0
set parse_number_result=
:parse_number_loop
set last_three=%number:~-3%
set number=%number:~0,-3%
call :parse_three %last_three%
call :get_from %counter% %big_numbers%
if "$%get_from_result%" == "$" (
set parse_number_result=* ERR: the number is too big! Even wikipedia doesn't know how it's called!
exit /B 0
)
if not "$%parse_three_result%" == "$Zero" (
if %counter% == 0 (
set parse_number_result=%parse_three_result%
) else (
if not "$%parse_number_result%" == "$" (
set parse_number_result=%parse_three_result% %get_from_result% %parse_number_result%
) else (
set parse_number_result=%parse_three_result% %get_from_result%
)
)
)
set /A counter+=1
if not "$%number%" == "$" goto parse_number_loop
if "$%parse_number_result%" == "$" (
set parse_number_result=Zero
exit /B 0
) else if not "$%number_sign%" == "$" (
set parse_number_result=%number_sign% %parse_number_result%
)
exit /B 0
:parse_three
call :trim_leading_zeros %~1
set three=%trim_leading_zeros_result%
set /A three=%three% %% 1000
set /A two=%three% %% 100
call :parse_two %two%
set parse_three_result=
set /A digit=%three% / 100
if not "$%digit%" == "$0" (
call :get_from %digit% %zero_to_nineteen%
)
if not "$%digit%" == "$0" (
if not "$%get_from_result%" == "$Zero" (
set parse_three_result=%get_from_result% Hundred
)
)
if "$%parse_two_result%" == "$Zero" (
if "$%parse_three_result%" == "$" (
set parse_three_result=Zero
)
) else (
if "$%parse_three_result%" == "$" (
set parse_three_result=%parse_two_result%
) else (
set parse_three_result=%parse_three_result% %parse_two_result%
)
)
exit /B 0
:parse_two
call :trim_leading_zeros %~1
set two=%trim_leading_zeros_result%
set /A two=%two% %% 100
call :get_from %two% %zero_to_nineteen%
if not "$%get_from_result%" == "$" (
set parse_two_result=%get_from_result%
goto parse_two_20_end
)
set /A digit=%two% %% 10
call :get_from %digit% %zero_to_nineteen%
set parse_two_result=%get_from_result%
set /A digit=%two% / 10
call :get_from %digit% %twenty_to_ninety%
if not "$%parse_two_result%" == "$Zero" (
set parse_two_result=%get_from_result% %parse_two_result%
) else (
set parse_two_result=%get_from_result%
)
goto parse_two_20_end
:parse_two_20_end
exit /B 0
:get_from
call :trim_leading_zeros %~1
set idx=%trim_leading_zeros_result%
set /A idx=0+%~1
shift
:get_from_loop
if "$%idx%" == "$0" goto get_from_loop_end
set /A idx-=1
shift
goto get_from_loop
:get_from_loop_end
set get_from_result=%~1
exit /B 0
:trim_leading_zeros
set str=%~1
set trim_leading_zeros_result=
:trim_leading_zeros_loop
if not "$%str:~0,1%" == "$0" (
set trim_leading_zeros_result=%trim_leading_zeros_result%%str%
exit /B 0
)
set str=%str:~1%
if not "$%str%" == "$" goto trim_leading_zeros_loop
if "$%trim_leading_zeros_result%" == "$" set trim_leading_zeros_result=0
exit /B 0
:get_sign
set str=%~1
set sign=%str:~0,1%
set get_sign_result=
if "$%sign%" == "$-" (
set get_sign_result=Minus
set get_sign_result_number=%str:~1%
) else if "$%sign%" == "$+" (
set get_sign_result_number=%str:~1%
) else (
set get_sign_result_number=%str%
)
exit /B 0
:remove_groups
set str=%~1
set remove_groups_result=%str:'=%
exit /B 0
This is the test script I used:
@echo off
rem 10^x:x= 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
call number 0
call number 2
call number -17
call number 30
call number 48
call number -256
call number 500
call number 874
call number 1'024
call number -17'001
call number 999'999
call number 1'048'576
call number -1'000'001'000'000
call number 912'345'014'587'957'003
call number -999'912'345'014'587'124'337'999'999
call number 111'222'333'444'555'666'777'888'999'000'000'000'001
call number -912'345'014'587'912'345'014'587'124'912'345'014'587'124'337
call number 999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999
call number 1'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000
rem 10^x:x= 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
And this is the output I got from my test script:
0 -> Zero
2 -> Two
-17 -> Minus Seventeen
30 -> Thirty
48 -> Forty Eight
-256 -> Minus Two Hundred Fifty Six
500 -> Five Hundred
874 -> Eight Hundred Seventy Four
1'024 -> One Thousand Twenty Four
-17'001 -> Minus Seventeen Thousand One
999'999 -> Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
1'048'576 -> One Million Forty Eight Thousand Five Hundred Seventy Six
-1'000'001'000'000 -> Minus One Trillion One Million
912'345'014'587'957'003 -> Nine Hundred Twelve Quadrillion Three Hundred Forty Five Trillion Fourteen Billion Five Hundred Eighty Seven Million Nine Hundred Fifty Seven Thousand Three
-999'912'345'014'587'124'337'999'999 -> Minus Nine Hundred Ninety Nine Septillion Nine Hundred Twelve Sextillion Three Hundred Forty Five Quintillion Fourteen Quadrillion Five Hundred Eighty Seven Trillion One Hundred Twenty Four Billion Three Hundred Thirty Seven Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
111'222'333'444'555'666'777'888'999'000'000'000'001 -> One Hundred Eleven Undecillion Two Hundred Twenty Two Decillion Three Hundred Thirty Three Nonillion Four Hundred Forty Four Octillion Five Hundred Fifty Five Septillion Six Hundred Sixty Six Sextillion Seven Hundred Seventy Seven Quintillion Eight Hundred Eighty Eight Quadrillion Nine Hundred Ninety Nine Trillion One
-912'345'014'587'912'345'014'587'124'912'345'014'587'124'337 -> Minus Nine Hundred Twelve Tredecillion Three Hundred Forty Five Duodecillion Fourteen Undecillion Five Hundred Eighty Seven Decillion Nine Hundred Twelve Nonillion Three Hundred Forty Five Octillion Fourteen Septillion Five Hundred Eighty Seven Sextillion One Hundred Twenty Four Quintillion Nine Hundred Twelve Quadrillion Three Hundred Forty Five Trillion Fourteen Billion Five Hundred Eighty Seven Million One Hundred Twenty Four Thousand Three Hundred Thirty Seven
999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999 -> Nine Hundred Ninety Nine Vigintillion Nine Hundred Ninety Nine Novemdecillion Nine Hundred Ninety Nine Octodecillion Nine Hundred Ninety Nine Septendecillion Nine Hundred Ninety Nine Sexdecillion Nine Hundred Ninety Nine Quindecillion Nine Hundred Ninety Nine Quattuordecillion Nine Hundred Ninety Nine Tredecillion Nine Hundred Ninety Nine Duodecillion Nine Hundred Ninety Nine Undecillion Nine Hundred Ninety Nine Decillion Nine Hundred Ninety Nine Nonillion Nine Hundred Ninety Nine Octillion Nine Hundred Ninety Nine Septillion Nine Hundred Ninety Nine Sextillion Nine Hundred Ninety Nine Quintillion Nine Hundred Ninety Nine Quadrillion Nine Hundred Ninety Nine Trillion Nine Hundred Ninety Nine Billion Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
1'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000 -> * ERR: the number is too big! Even wikipedia doesn't know how it's called!
If I could find some more names of large numbers, the script would support even bigger numbers. Currently, though, the script will work with all numbers from -(10^66-1) to (10^66-1).
I have to mention, that I had a lot of fun solving this in BATCH. :)
C# - 30 lines incl. method declaration and { }s:
Takes into account all the previously aforementioned commas, ands and hyphens. I've only included up to octillion because decimal.MaxValue is only in the octillions. For bigger integers you would need to add corresponding items to the thou[] array and perhaps pass the number in as a string, modifying the line to extract the block by using the last 3 chars instead of using modulo as I have here.
static string wordify(decimal v)
{
if (v == 0) return "zero";
var units = " one two three four five six seven eight nine".Split();
var teens = " eleven twelve thir# four# fif# six# seven# eigh# nine#".Replace("#", "teen").Split();
var tens = " ten twenty thirty forty fifty sixty seventy eighty ninety".Split();
var thou = " thousand m# b# tr# quadr# quint# sext# sept# oct#".Replace("#", "illion").Split();
var g = (v < 0) ? "minus " : "";
var w = "";
var p = 0;
v = Math.Abs(v);
while (v > 0)
{
int b = (int)(v % 1000);
if (b > 0)
{
var h = (b / 100);
var t = (b - h * 100) / 10;
var u = (b - h * 100 - t * 10);
var s = ((h > 0) ? units[h] + " hundred" + ((t > 0 | u > 0) ? " and " : "") : "")
+ ((t > 0) ? (t == 1 && u > 0) ? teens[u] : tens[t] + ((u > 0) ? "-" : "") : "")
+ ((t != 1) ? units[u] : "");
s = (((v > 1000) && (h == 0) && (p == 0)) ? " and " : (v > 1000) ? ", " : "") + s;
w = s + " " + thou[p] + w;
}
v = v / 1000;
p++;
}
return g + w;
}
Called using:
static void Main(string[] args)
{
Console.WriteLine(wordify(decimal.MaxValue));
}
Output:
seventy-nine octillion, two hundred and twenty-eight septillion, one hundred and sixty-two sextillion, five hundred and fourteen quintillion, two hundred and sixty-four quadrillion, three hundred and thirty-seven trillion, five hundred and ninety-three billion, five hundred and forty-three million, nine hundred and fifty thousand, three hundred and thirty-five
Hi!
I'm looking for a third party library in either Java or JavaScript that can convert numbers to words.
On the surface this isn't a majorly complex task, but my app will run inside documents so it needs to support all sorts of number types and anything else that can go with it. For example, numbers can be phone numbers, ZIP codes or just plain numbers. You can also have other symbols and characters beside the numbers like currency symbols and units of measurements. Is there anything out there that can convert this to words as well?
Examples Input - I have $3 Output - I have three dollars
Input - My ZIP code is 12345 and my car speed is 70mph. Output - My ZIP code is one two three four five and my car speed is seventy miles per hour.
Are you allowed to use other packages? This one works really well for me: Inflect. It is useful for natural language generation and has a method for turning numbers into english text.
I installed it with
$ pip install inflect
Then in your Python session
>>> import inflect
>>> p = inflect.engine()
>>> p.number_to_words(1234567)
'one million, two hundred and thirty-four thousand, five hundred and sixty-seven'
>>> p.number_to_words(22)
'twenty-two'
You can make this much simpler by using one dictionary and a try/except clause like this:
num2words = {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', 0: 'Zero'}
>>> def n2w(n):
try:
print num2words[n]
except KeyError:
try:
print num2words[n-n%10] + num2words[n%10].lower()
except KeyError:
print 'Number out of range'
>>> n2w(0)
Zero
>>> n2w(13)
Thirteen
>>> n2w(91)
Ninetyone
>>> n2w(21)
Twentyone
>>> n2w(33)
Thirtythree
Is it ok to struggle in programming a number to words converter? I tend to spend lots of time on such challenges and I'm pretty slow in finding solutions, is that a normal thing? Or am I just stupid for programming? I would like to hear from an experienced software engineer.
Edit: so many have misunderstood the task, by converting numbers to words I mean converting 1234567 to one million two hundred thirty-four thousand five hundred sixty-seven. It's basically like this: https://www.calculatorsoup.com/calculators/conversions/numberstowords.php