## Num2Text function

Post your working scripts, libraries and tools
sinkfaze
Posts: 589
Joined: 01 Oct 2013, 08:01

### Num2Text function

I saw it mentioned somewhere on the forums that someone was trying to translate numbers into their text equivalents, so I decided to take a stab at the problem with my own function. It has been tested but not rigorously, and it doesn't feel like it's as efficient as it should be, so any suggestions on how to improve it are welcome. This function only work on whole numbers greater than 0 and less than 1 trillion.

Code: Select all

``````MsgBox %	Num2Text(4)
MsgBox %	Num2Text(14014)
MsgBox %	Num2Text(565096364935)

Num2Text(n) {

static	_tens :=	["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
static	tens :=		{20:"twenty",30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety"}
static	suf :=		{2:" thousand",3:" million",4:" billion"}

StringReplace, n, n, `,, , All		; remove all commas

; ##### PRE-PROCESS ERROR CHECKING #####

if	!RegExMatch(n,"^\d+\$")			; if var is not valid
{
MsgBox, 48, Warning!, The entry is either not valid or is not a whole number.
return	""
}
if	(n > 999999999999) || (n < 1)	; if var falls outside func bounds
{
MsgBox, 48, Warning!, This function will only convert numbers between 0 and 1 trillion.
return	""
}

; ##### NUMBER SEPARATION #####

tSep :=	[]
Loop %	StrLen(n) // 3
tSep.Push(SubStr(n,-2)), n :=	SubStr(n,1,StrLen(n)-3)
if	m :=	Mod(StrLen(n),3)
tSep.Push(SubStr(n,1,m))

; ##### NUMBER-TO-TEXT ANALYSIS #####

out :=	""
For i, c in tSep
{
c *= 1, text :=	""
if	!c
continue
if	(c < 100)
if	!_tens[c]
if	!tens[c]
text :=	tens[SubStr(c,1,1)*10] "-" _tens[SubStr(c,0)]
else	text :=	tens[c]
else	text :=	_tens[c]
else if	!Mod(c,100)
text :=	_tens[SubStr(c,1,1)] " hundred"
else if	!Mod(c,10)
text :=	_tens[SubStr(c,1,1)] " hundred " tens[SubStr(c,-1)]
else if	_tens[SubStr(c,-1)*1]
text :=	_tens[SubStr(c,1,1)] " hundred " _tens[SubStr(c,-1)*1]
else	text :=	_tens[SubStr(c,1,1)] " hundred " tens[SubStr(c,2,1)*10] "-" _tens[SubStr(c,0)]
out :=	text suf[i] " " out
}
return	out
}``````
ahk7
Posts: 156
Joined: 06 Nov 2013, 16:35

### Re: Num2Text function

jeeswg
Posts: 6125
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: Num2Text function

I had a go here. Cheers.
Amount to word business stating writing? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=36646&p=168837#p168837

An example:

UNIX: Making Computers Easier To Use -- AT&T Archives film from 1982, Bell Laboratories - YouTube

2**100 = 1267650600228229401496703205376

from my function: one nonillion, two hundred and sixty-seven octillion, six hundred and fifty septillion, six hundred sextillion, two hundred and twenty-eight quintillion, two hundred and twenty-nine quadrillion, four hundred and one trillion, four hundred and ninety-six billion, seven hundred and three million, two hundred and five thousand, three hundred and seventy-six
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
wolf_II
Posts: 2508
Joined: 08 Feb 2015, 20:55

### Re: Num2Text function

This is code from AHK forum user Laszlo. I tweaked it a bit (to avoid the transformation of s to a number)
I also turned it around to answer a user request. sadly, i can not find the thread.

Code: Select all

``````

;-------------------------------------------------------------------------------
Spell(n) { ; recursive function to spell out the name of a max 36 digit integer
;-------------------------------------------------------------------------------
; credit to AHK forum user Laszlo
;---------------------------------------------------------------------------
Static p1 := "thousand", p2 := "million", p3 := "billion", p4 := "trillion"
, p5 := "quadrillion", p6 := "quintillion", p7 := "sextillion"
, p8 := "septillion", p9 := "octillion", p10 := "nonillion"
, p11 := "decillion"

, t2 := "twenty", t3 := "thirty", t4 := "forty", t5 := "fifty"
, t6 := "sixty", t7 := "seventy", t8 := "eighty", t9 := "ninety"

, o0 := "zero", o1 := "one", o2 := "two", o3 := "three", o4 := "four"
, o5 := "five", o6 := "six", o7 := "seven", o8 := "eight", o9 := "nine"
, o10 := "ten", o11 := "eleven", o12 := "twelve", o13 := "thirteen"
, o14 := "fourteen", o15 := "fifteen", o16 := "sixteen"
, o17 := "seventeen", o18 := "eighteen", o19 := "nineteen"

n := RegExReplace(n, "^0+(\d)", "\$1") ; remove surplus leading 0s from n

If (d := (StrLen(n) - 1) // 3) > 11 ; # of digit groups of 3
Return, "Number too big"

If d ; more than 3 digits
Return, Spell(SubStr(n, 1, -3 * d)) " " p%d%
; avoid the transformation of s to a number
. ((s := SubStr(n, 1 - 3 * d)) ? ", " Spell(s "") : "")

i := SubStr(n, 1, 1)
If (n > 99) ; 3 digits
Return, o%i% " hundred" ((s := SubStr(n, 2)) ? " and " Spell(s) : "")

If (n > 19) ; n = 20..99
Return, t%i% ((o := SubStr(n, 2)) ? "-" o%o% : "")

Return, o%n% ; n = 0..19
}``````
rommmcek
Posts: 517
Joined: 15 Aug 2014, 15:18

### Re: Num2Text function

Thanks guys, I just needed this!
@ wolf_II: May be this one
@ sinkfaze: Your function gives me five hundred sixty-five billion - million three hundred sixty-four thousand nine hundred thirty-five for 565096364935. Is there a bug?
wolf_II
Posts: 2508
Joined: 08 Feb 2015, 20:55

### Re: Num2Text function

Unfortunately, the OP did not feed back to us, but with the help of HelgeF, I managed to put together a version that does something like "TextToNum".
@rommmcek: Thanks for the link, I completely forgot that one.
rommmcek
Posts: 517
Joined: 15 Aug 2014, 15:18

### Re: Num2Text function

Interesting! I extended your function to handle output formats of this thread.
sinkfaze
Posts: 589
Joined: 01 Oct 2013, 08:01

### Re: Num2Text function

rommmcek wrote:
11 Feb 2019, 20:38
@ sinkfaze: Your function gives me five hundred sixty-five billion - million three hundred sixty-four thousand nine hundred thirty-five for 565096364935. Is there a bug?
No, just bad copying and pasting from my tester function, it's fixed now. Sorry about that!