Jump to content

Reverse _NumberNumToName


Recommended Posts

English not being my mothertongue I wonder when one should use "one" before "hundred" is various conditions.

One hundred vs hundred
One hundred five vs hundred five
Five thousand one hundred vs five thousand hundred

I believe the rule for hundred isn't the same as for thousand but I may be wrong.

Anyway if you want one before hundred just replace the block as below:

-----------------
        Switch $iHundreds
            Case 2 To 9
                $sText &= $aWords[0][$iHundreds]
                ContinueCase
            Case 1
                $sText &= " hundred"
        EndSwitch
-----------------
        $sText &= $aWords[0][$iHundreds] & " hundred"
-----------------

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I cant think of any time when it is used without the 'one',  all of the items on the right side of your -vs- sound incorrect.  even colloquially I would replace the 'one' with an 'a', not just leave it off entirely.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

OK noted.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • Moderators

Hi,

My back-and-forth version - optional delimiter for the digits and optional "and"  in the text (required for the proper English speakers, of course):

#include <StringConstants.au3>

Local $aMajorDivisions[4] = ["", "thousand", "million", "billion"]
Local $aDecades[10] = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
Local $aTens[10] = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
Local $aUnits[10] = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

; ###########################

; Example

Local $aInitialAmounts[] = ["One billion, nine hundred eighty-one million, four hundred nine thousand, one hundred seventy-nine", _
        "One hundred and three billion two hundred and thirteen thousand seven hundred and fifty five", _
        "Three billion eleven million nineteen thousand and one", _
        "Five hundred and thirteen thousand seven hundred and fifty five", _
        "Five hundred and thirteen thousand"]

For $n = 0 To UBound($aInitialAmounts) - 1
    $sDigitString = _TextToDigits($aInitialAmounts[$n], ",")
    $sTextString = _DigitsToText($sDigitString, ",")
    ConsoleWrite($aInitialAmounts[$n] & @CRLF & $sDigitString & @CRLF & $sTextString & @CRLF & @CRLF)
Next

; ###########################

Func _DigitsToText($sDigitString, $sDelimiter = "", $bAnd = True)

    Local $sAnswer, $iDivisions, $sHundreds, $sRemainder, $aSections[1]

    ; Set "and" value
    $sAnd = (($bAnd) ? ("and ") : (""))

    ; Remove any delimiters
    $sDigitString = StringRegExpReplace($sDigitString, "\D", "")

    If StringLen($sDigitString) = 1 Then
        $sAnswer = $aUnits[$sDigitString]
    ElseIf StringLen($sDigitString) = 2 Then
        $sAnswer = _UpTo100($sDigitString, $aUnits)
    Else
        $sAnswer = ""
        $iDivisions = Ceiling(StringLen($sDigitString) / 3)
        Dim $aSections[$iDivisions + 1]
        For $i = 0 To $iDivisions
            $aSections[$i] = StringRight($sDigitString, 3)
            $sDigitString = "00" & StringTrimRight($sDigitString, 3)
        Next

        For $i = $iDivisions - 1 To 0 Step -1
            If $aSections[$i] <> "000" Then
                If StringLen($aSections[$i]) = 3 Then
                    $sHundreds = StringLeft($aSections[$i], 1)
                    $sRemainder = StringRight($aSections[$i], 2)
                Else
                    $sHundreds = "0"
                    $sRemainder = $aSections[$i]
                EndIf

                If $sHundreds > 0 Then
                    If $sRemainder = "00" Then
                        $sAnswer &= $aUnits[$sHundreds] & " hundred"
                    Else
                        $sAnswer &= $aUnits[$sHundreds] & " hundred " & $sAnd
                    EndIf
                Else
                    If $sRemainder <> "00" And $i = 0 Then
                        $sAnswer &= $sAnd
                    EndIf
                EndIf
                $sAnswer &= _UpTo100($sRemainder, $aUnits) & $aMajorDivisions[$i] & (($i = 0) ? ("") : ($sDelimiter)) & " "
            EndIf
        Next

    EndIf

    Return $sAnswer

EndFunc   ;==>_DigitsToText

Func _UpTo100($sDigitString, $aUnits)

    Local $sAnswer = ""
    Local $iTens = Number(StringLeft($sDigitString, 1))
    Local $iUnits = Number(StringRight($sDigitString, 1))
    Switch $iTens
        Case 0
            $sAnswer = $aUnits[$iUnits] & " "
        Case 1
            $sAnswer = $aTens[$iUnits] & " "
        Case 2 To 9
            $sAnswer = $aDecades[$iTens] & " " & $aUnits[$iUnits] & " "
    EndSwitch

    Return $sAnswer

EndFunc   ;==>_UpTo100

Func _TextToDigits($sInitialAmount, $sDelimiter = "")

    ; Remove any English "and"s
    $sWorkingAmount = StringReplace($sInitialAmount, " and ", " ")
    ; Initialise return
    $sFigures = ""

    For $i = 3 To 1 Step -1
        ; Look for major division (billion, million, thousand)
        $iPos = StringInStr($sWorkingAmount, $aMajorDivisions[$i])
        If $iPos Then
            ; Get value of major division
            $sValue = StringMid($sWorkingAmount, 1, $iPos - 1)
            ; And parse it
            $sFigures &= _ParseHundred($sValue) & $sDelimiter
            ; Strip to next division
            $sWorkingAmount = StringTrimLeft($sWorkingAmount, $iPos + StringLen($aMajorDivisions[$i]))
        Else
            ; No major division
            If $sFigures Then
                $sFigures &= "000" & $sDelimiter
            EndIf
        EndIf
    Next
    ; Parse any remaining hundred value
    If $sWorkingAmount Then
        $sFigures &= _ParseHundred($sWorkingAmount)
    Else
        $sFigures &= "000"
    EndIf

    ; Strip any leading zeroes
    While StringLeft($sFigures, 1) = 0
        $sFigures = StringTrimLeft($sFigures, 1)
    WEnd

    Return $sFigures

EndFunc   ;==>_TextToDigits

Func _ParseHundred($sMajorValue)

    ; Initialise return
    $sIntFigures = ""
    ; Initialise unit value
    $sUnits = ""

    ; Look for "hundred"
    $aSplit = StringSplit($sMajorValue, "hundred", $STR_ENTIRESPLIT)
    If @error Then
        ; Add "0" if no hundred
        $sIntFigures &= "0"
        ; Set unit value
        $sUnits = $sMajorValue
    Else
        ; Parse hundred value
        For $j = 1 To 9
            If StringInStr($aSplit[1], $aUnits[$j]) Then
                ; Add hundred value
                $sIntFigures &= $j
                ; Set unit value
                $sUnits = $aSplit[2]
                ExitLoop
            EndIf
        Next
    EndIf

    ; Parse unit value
    ; Look for 10-19
    For $j = 0 To 9
        If StringInStr($sUnits, $aTens[$j]) Then
            ; Add value
            $sIntFigures &= "1" & $j
            ExitLoop
        EndIf
    Next
    ; If not found then look for decades
    If $j > 9 Then
        For $j = 2 To 9
            If StringInStr($sUnits, $aDecades[$j]) Then
                ; Add value
                $sIntFigures &= $j
                ; Remove decade from string
                $sUnits = StringReplace($sUnits, $aDecades[$j], "")
                ExitLoop
            EndIf
        Next
        If $j > 9 Then
            ; Add "0"  if no ten value
            $sIntFigures &= "0"
        EndIf
        ; Finally look for units
        For $j = 1 To 9
            If StringInStr($sUnits, $aUnits[$j]) Then
                ; Add value
                $sIntFigures &= $j
                ExitLoop
            EndIf
        Next
        ; Add "0" if no unit value
        If $j > 9 Then
            $sIntFigures &= "0"
        EndIf
    EndIf

    Return $sIntFigures

EndFunc   ;==>_ParseHundred

As I said yesterday - a fun little project to fill in a few hours.

M23

Edited by Melba23
Amended code - see below

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

the line below also appears to be a good form for an output

_TextToDigits("one billion, nine hundred eighty-one million, four hundred nine thousand, one hundred seventy-nine")

returned : 1981409177

 _DigitsToText(1981409179) ; checked right

Link to comment
Share on other sites

  • Moderators

Deye,

Good spot - try it now. And you can also now get the commas added to the text version - I am not adding the hyphens as they are not used in proper English numbering.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 4 weeks later...

Had to have another crack on this,  .. only wanted to see how was it doable all in reverse mod.. anyways:

Global $aArray[4][10] = [['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'], ['', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'], ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'], ['hundred', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion']]

$iNumber = "40080090010022213"
$iName = "forty quadrillion, eighty trillion, ninety billion, ten million, twenty-two thousand, two hundred thirteen"
$iResult = _StringToDigits($iName)
If $iResult = $iNumber Then MsgBox(0, " -- " & $iResult & " -- ", $iName)

Func _StringToDigits($iNumber)
    Local $iRet, $i, $iDiv, $iPast_Div, $iNext_Div
    Local $aNum = StringSplit(StringStripWS(StringReplace(StringReplace($iNumber, "-", " ", 0), ",", " ", 0), 6), " ", 1)
    For $1 = $aNum[0] To 0 Step -1
        If $aNum[$1] = "and" Then ContinueLoop
        If $1 = 0 Then
            $iRet = $i & (StringLen($iRet) > StringLen($iNext_Div) ? StringTrimRight($iPast_Div & $iNext_Div, StringLen($iRet)) : StringTrimRight($iNext_Div, StringLen($iRet))) & $iRet
            Return $iRet
        EndIf
        Local $a = Execute(StringRegExpReplace($aNum[$1], "^.*?(?:(elv|elev|teen)|(ten|ty)|(san|red|ion)).*", "('$1'=''?0:1)+('$2'=''?0:2)+('$3'=''?0:3)"))
        If Not $a Then $a = 0
        For $b = 0 To 9
            If $aNum[$1] = $aArray[$a][$b] Then
                Switch $a
                    Case 0
                        $i = $b
                    Case 1
                        $i = 1 & $b
                    Case 2
                        If $i Then
                            $i = $b & $i
                        Else
                            $i = $b & 0
                        EndIf
                    Case 3
                        $iNext_Div = ""
                        If $b > 1 Then
                            For $k = $b To 1 Step -1
                                $iNext_Div &= "000"
                            Next
                        Else
                            $iNext_Div = ($b = 1 ? "000" : "00")
                        EndIf

                        If $iDiv Then
                            If $i Then
                                $iRet = (StringLen($iPast_Div) > StringLen($iRet) ? $i & StringTrimLeft($iNext_Div, StringLen($i & $iRet)) : (StringLen($iDiv) >= StringLen($iRet) ? $i & StringTrimLeft($iDiv, StringLen($iRet)) : (StringLen(StringTrimLeft($iNext_Div, StringLen($iRet & $iPast_Div))) >= 1 ? $i & StringTrimLeft(StringTrimLeft($iNext_Div, StringLen($iRet & $iPast_Div)), StringLen($i)) : (StringLen($iRet) - StringLen($iDiv & $iPast_Div) = 0 ? StringTrimLeft($iNext_Div, StringLen($i & $iRet)) & $i : $i & StringTrimLeft($iNext_Div, StringLen($i & $iRet)))))) & $iRet
                            Else
                                $iRet = $iNext_Div & $iRet
                            EndIf
                        Else
                            $iRet = $i
                        EndIf
                        $i = ""
                        $iPast_Div = $iDiv
                        $iDiv = $iNext_Div
                EndSwitch
                ContinueLoop 2
            EndIf
        Next
    Next
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...