Numbers to Words - Dollars and Cents Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nicholas2450
Posts: 2
Joined: 27 May 2022, 17:38

Numbers to Words - Dollars and Cents

Post by Nicholas2450 » 27 May 2022, 18:10

I am interested in doing similar item to the below statement:

After the number, I type 'ntt' (of Number To Text), then I press the space bar twice and the number which I've just typed, is automatically converted to it's text form. (Which was taken from https://www.autohotkey.com/board/topic/123215-convert-number-to-text/).

I have also read the below posts for a similar problem
viewtopic.php?p=168685#p168685

My issue is how? whilst these are all the great functions, I have no idea how to use it so that the number to be converted is selected from whatever program I am using, & outputted back into it? In the first example it appears to work only with integers. In the second example it appears the number is coded into it. My desire is to have something work with dollars and cents.

Apologies for my lack on knowledge.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Numbers to Words - Dollars and Cents  Topic is solved

Post by flyingDman » 27 May 2022, 19:15

For grabbing a number from whatever program you are using and pasting it back, there are plenty of examples on the forum.
Regarding the function that I used and borrowed from Laszlo in the thread you quoted, it can easily be converted to split a decimal number into 2 parts like so:

Code: Select all

n = 123456.12
n1 := strsplit(n,".").1
n2 := strsplit(n,".").2
MsgBox 1, Number Names, % Spell(n1) " dollars and " Spell(n2) " cents"
return

Spell(n) { ; recursive function to spell out the name of a max 36 digit integer, after leading 0s removed
    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)) ? " and " Spell(s) : "")

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

    Return o%n%                        ; n = 0..19
}
To put in the form of a hotstring, I would still recommend the Hotstrings() function see here: https://www.autohotkey.com/board/topic/114764-regex-dynamic-hotstrings/). Try:

Code: Select all

hotstrings("(\d+\.?\d*)ntt\s\s", "lbl")
return

lbl:
n1 := strsplit($1,".").1, n2 := strsplit($1,".").2
n2 := !n2 ? 0 : n2
send % spell(n1) " dollars and " Spell(n2) " cents"
return
14.3 & 1.3.7

Nicholas2450
Posts: 2
Joined: 27 May 2022, 17:38

Re: Numbers to Words - Dollars and Cents

Post by Nicholas2450 » 27 May 2022, 22:27

Hi flyingDman,

Whilst your instructions for the fix were largely clear, I still struggled for a while to understand. I eventually worked out that I need to create/add a HotStrings.ahk (thanks for the link) even though you included the information. Obviously, I am a slow learner.
Your assistance (understatement, you did it all!!, my part was to have the problem) has been greatly appreciated.

Thank you

Post Reply

Return to “Ask for Help (v1)”