Convert numbers to text

Suggestions, feature requests and bug reports.
Post Reply
sonif28
Posts: 1
Joined: May 20th, ’24, 22:14

Post by sonif28 » May 20th, ’24, 22:23

Greetings!
A function for hotkeys is required - converting numbers to text.
Those. Often you have to write, for example, “1,000,000 (One million) rubles 00 kopecks.”
So, the ability to automate such text after writing the numbers is required.
I write - 1,000,000, press the hot key and next to the written numbers the number is automatically written in the text - '(One million)'.

Is it possible to implement such a function? How to do it?
I would be very grateful for your help.
User avatar
Marko
Posts: 1756
Joined: Mar 2nd, ’13, 21:02

Post by Marko » May 25th, ’24, 13:19

Try this as a shortcut (Type: Command). Script will read the number left from the caret and add spelled text

Code: Select all

send, ^+{Left}
send ^c
send {right}
sleep 100
n1 := strsplit(clipboard,".").1
n2 := strsplit(clipboard,".").2
stringreplace, n1, n1, `,,, all
send, % " - (" Spell(n1) " dollars and " Spell(n2) " cents)"
return


Spell(n) {
    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 leading 0s from n

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

    If (d)                             ; more than 3 digits
        Return Spell(SubStr(n,1,-3*d)) p%d% ((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)) ? " " Spell(s) : "")

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

    Return o%n%                        ; n = 0..19
}
return

Based on the function found here:
https://www.autohotkey.com/boards/viewt ... p?t=104617
Post Reply