Jump to content

Locale Questions


Recommended Posts

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Replies 54
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Bug Trac #442 just got closed and there's a new _StringAddThousandsSep() in the 3.3.1.0 Beta, but it's not the one I was hoping for <pout>, nor do I think it is yours? It's gotten big (ok, slow) and has a mechanism for converting from one locale to another which works well (although a script-breaker). It doesn't try to enforce valid input data, so garbage-in gets garbage-out, but the prod version was the same. Leaving the programmer to ensure a valid input is no big deal. I'm not sure how important it is, but it doesn't like values between 0 and 1, as the output from the 4th and 5th tests below are not what one would expect.

The help file has been updated, but still refers to the function parameters as "$sDecimal" and "$sThousands", which is no longer the case. I found the description for the third parm confusing "else LCID number was converted with originally". Not sure what might be clearer, maybe "else LCID (format) of input string"?

#include<string.au3>

Test("99999")
Test("-000123456789012345678901.12345")
Test("a-001b2345e.12g5h")
Test("-0.5")
Test(".666")

Func Test($amt)
    $timer = TimerInit()
    For $x = 1 to 1000
        $result = ___StringAddThousandsSep($amt)
    Next
    $timer = TimerDiff($timer)
    $result2 = "A fast version        Time: " & StringFormat('%.2f', $timer) & "    Result:  " & $result

    $timer = TimerInit()
    For $x = 1 to 1000
        $result = _____StringAddThousandsSep($amt)
    Next
    $timer = TimerDiff($timer)
    $result3 = "Beta 3.1.1             Time : " & StringFormat('%.2f', $timer) & "  Result:  " & $result

    $timer = TimerInit()
    For $x = 1 to 1000
        $result = _StringAddThousandsSep($amt)
    Next
    $timer = TimerDiff($timer)
    $result4 = "Production version  Time: " & StringFormat('%.2f', $timer) & "  Result:  " & $result

    Msgbox(0, "Results","Input: " & $amt & @CRLF &  @CRLF & $result2 & @CRLF & $result3 & @CRLF & $result4)
EndFunc

; ==============================================================================
; new function without kernel32
Func ___StringAddThousandsSep($sText, $sThousands = ",", $sDecimal = ".")
    If Not (StringIsInt($sText) Or StringIsFloat($sText)) Then Return SetError(1,0,"")
    Local $Neg = ""
    If $sDecimal = -1 Then
        DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", 0, "int", 0x0E, "str", $sDecimal, "int", 2)
    EndIf
    If $sThousands = -1 Then
        DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", 0, "int", 0x0F, "str", $sThousands, "int", 2)
    EndIf       
    $sText = StringRegExpReplace($sText, "\-", "")
    If @extended Then $Neg = "-"
    Local $aSplit= StringSplit($sText, $sDecimal)   
    $aSplit[1] = StringRegExpReplace($aSplit[1], "(\A0+)", ""); Strip leading zeros
    If Not $aSplit[1] Then $aSplit[1] = "0"; insert zero in front of decimal (or not?)
    Local $iMod = Mod(StringLen($aSplit[1]), 3)
    If Not $iMod Then $iMod = 3;  0 --> 3
    $aSplit[1] = StringRegExpReplace($aSplit[1], "(?<=\d{" & $iMod & "})(\d{3})", $sThousands & "\1")   
    If $aSplit[0] > 1 Then $aSplit[1] &= "." & $aSplit[2]
    Return $Neg & $aSplit[1]
EndFunc

; ==============================================================================
Func _____StringAddThousandsSep($s_string, $i_convert_lcid = -1, $i_current_lcid = -1)
 ; $LOCALE_USER_DEFAULT = 0x0400
    If $i_current_lcid = -1 Or $i_current_lcid = Default Then $i_current_lcid = 0x0400
    If $i_convert_lcid = -1 Or $i_convert_lcid = Default Then $i_convert_lcid = 0x0400

 ; Get lcid decimal and thousands separators
    Local $t_buff_tmp = DllStructCreate("char[4]")
    DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", $i_current_lcid, _
        "int", 0x0E, "ptr", DllStructGetPtr($t_buff_tmp), "int", 4); decimal sign
    If @error Then Return SetError(1, 0, "")
    Local $s_cur_dec = DllStructGetData($t_buff_tmp, 1)

    DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", $i_convert_lcid, _
        "int", 0x0E, "ptr", DllStructGetPtr($t_buff_tmp), "int", 4); decimal sign
    If @error Then Return SetError(1, 0, "")
    Local $s_con_dec = DllStructGetData($t_buff_tmp, 1)

    DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", $i_convert_lcid, _
        "int", 0x0F, "ptr", DllStructGetPtr($t_buff_tmp), "int", 4); thousands separator
    If @error Then Return SetError(1, 0, "")
    Local $s_con_tho = DllStructGetData($t_buff_tmp, 1)

 ; For later formatting
    Local $i_number = StringRegExpReplace($s_string, "(\" & $s_cur_dec & "\d+\z)|(^-|\d+)|(\D)", "$2")
    Local $i_dec = StringRegExpReplace($s_string, "(.+?\" & $s_cur_dec & ")(\d+\z)", "$2")
    If @extended = 0 Then $i_dec = ""
;   MsgBox(1,"",$i_number & "   " & $i_dec)

    Local $i_str_len = StringLen($s_string) * 4
    Local $t_numberfmt = DllStructCreate("uint;uint;uint;ptr;ptr;uint")
    Local $t_thousands = DllStructCreate("wchar[2]")
    Local $t_decimal = DllStructCreate("wchar[2]")
    Local $t_buffer = DllStructCreate("wchar[" & $i_str_len & "]")

    DllStructSetData($t_thousands, 1, $s_con_tho)
    DllStructSetData($t_decimal, 1, $s_con_dec)
    DllStructSetData($t_numberfmt, 3, 3)
    DllStructSetData($t_numberfmt, 4, DllStructGetPtr($t_decimal))
    DllStructSetData($t_numberfmt, 5, DllStructGetPtr($t_thousands))
    DllStructSetData($t_numberfmt, 6, 1)

    DllCall("kernel32.dll", "int", "GetNumberFormatW", _
        "int", $i_convert_lcid, "int", 0, _
        "wstr", $i_number, "ptr", DllStructGetPtr($t_numberfmt), _
        "ptr", DllStructGetPtr($t_buffer), "int", $i_str_len)

    If $i_dec = "" Then $s_con_dec = ""
    Return DllStructGetData($t_buffer, 1) & $s_con_dec & $i_dec
EndFunc ;==>_StringAddThousandsSep
Edited by Spiff59
Link to comment
Share on other sites

Spiff59, if you have a problem with how the new version works then you're going to end up having the function removed from the language. This functions is never going to make everybody happy. Everybody has an opinion on how it should work and what it should do. I'm about one more comment away from pulling it from the language because I am sick of it. It's been discussed on the issue tracker multiple times, in PM, in a private forum and in public. The next time I see it mentioned I'm pulling it from the language.

I am not joking.

Link to comment
Share on other sites

  • Moderators

Spiff59, if you have a problem with how the new version works then you're going to end up having the function removed from the language. This functions is never going to make everybody happy. Everybody has an opinion on how it should work and what it should do. I'm about one more comment away from pulling it from the language because I am sick of it. It's been discussed on the issue tracker multiple times, in PM, in a private forum and in public. The next time I see it mentioned I'm pulling it from the language.

I am not joking.

Shit, just do it! So tired of seeing shit about it, let them write their own versions that do specifically what they want. This 1 function has been re-written at least 3 times!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Shit, just do it! So tired of seeing shit about it, let them write their own versions that do specifically what they want. This 1 function has been re-written at least 3 times!

It is done. As of 3.3.1.1 _StringAddThousandsSep() will no longer be in the language. Good fucking riddance.
Link to comment
Share on other sites

  • 8 years later...

Hi ,

Looking for a simple english (dollar / cents)  version. Where can I find it.
Trying to do :

$input = 92.16
$output= Ninety Two Dollars and Sixteen cents

$input = 192.16
$output= One Ninety Two Dollars and Sixteen cents

$input = 192
$output= One Ninety Two Dollars

$input = 1,192.16
$output= One thousand one hundred and Ninety Two Dollars and sixteen cents

 

If there is a template code available , I can use it and modify it.

 

Link to comment
Share on other sites

  • Moderators

adityaparakh,

The code in posts 31 & 32 does not work for you?

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

3 minutes ago, Melba23 said:

adityaparakh,

The code in posts 31 & 32 does not work for you?

M23

 

31 does not work with decimals.
32 also shows errors -
Eg.92.16 - states "Ninety two thousand , sixteen"


I was hoping that there should be a solution available easy and specific for dollar and cents.
even if the format of output is not very strict , it should at least differentiate between before and after decimal.

 

Can you please assist , till $9999.99

Link to comment
Share on other sites

  • Moderators

adityaparakh,

Use StringSplit to separate the 2 sections so you can run the code on each part

I will look at the 2 scripts now to see if I can find out what is wrong with them.

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

  • Moderators

adityaparakh,

The first code in post #31 works perfectly for American English when I run it:

For $i = 1 To 9999
    ConsoleWrite(_N2T($i) & @CRLF)
Next

Func _N2T($iNum)
    Dim $lenNum, $arrNum, $chgNum, $numTxt
    Dim $boolTeens

    If Not(IsInt($iNum)) Then
        SetError(1)
        Return
    EndIf

    $lenNum = StringLen($iNum)
    $arrNum = StringSplit($iNum, "")

    If $lenNum = 1 Then
        If $arrNum[1] = 0 Then Return "Zero"
        If $arrNum[1] = 1 Then Return "One"
        If $arrNum[1] = 2 Then Return "Two"
        If $arrNum[1] = 3 Then Return "Three"
        If $arrNum[1] = 4 Then Return "Four"
        If $arrNum[1] = 5 Then Return "Five"
        If $arrNum[1] = 6 Then Return "Six"
        If $arrNum[1] = 7 Then Return "Seven"
        If $arrNum[1] = 8 Then Return "Eight"
        If $arrNum[1] = 9 Then Return "Nine"
    EndIf

    ;Assign $chgNum the length so the it can be changed.
    $chgNum = $lenNum

    For $i = 1 To $lenNum Step 1
        If Mod($chgNum, 3) = 0 Then ;Divisible by 3
            $numTxt &= _Ones($arrNum[$i])
            If $chgNum >= 3 AND Not($arrNum[$i] = 0) Then $numTxt &= "Hundred "
            $chgNum -= 1
        ElseIf Mod($chgNum, 3) = 2 Then
            If $arrNum[$i] = 1 Then
                If $arrNum[$i+1] = 0 Then $numTxt &= "Ten "
                If $arrNum[$i+1] = 1 Then $numTxt &= "Eleven "
                If $arrNum[$i+1] = 2 Then $numTxt &= "Twelve "
                If $arrNum[$i+1] = 3 Then $numTxt &= "Thirteen "
                If $arrNum[$i+1] = 4 Then $numTxt &= "Fourteen "
                If $arrNum[$i+1] = 5 Then $numTxt &= "Fifteen "
                If $arrNum[$i+1] = 6 Then $numTxt &= "Sixteen "
                If $arrNum[$i+1] = 7 Then $numTxt &= "Seventeen "
                If $arrNum[$i+1] = 8 Then $numTxt &= "Eighteen "
                If $arrNum[$i+1] = 9 Then $numTxt &= "Nineteen "
                $chgNum -= 1
                $boolTeens = 1
            Else
                $numTxt &= _Tens($arrNum[$i])
                $chgNum -= 1
            EndIf
        ElseIf Mod($chgNum, 3) = 1 Then
            If Not($boolTeens) Then $numTxt &= _Ones($arrNum[$i])
            $chgNum -= 1
            $boolTeens = 0
        EndIf

        If $lenNum = 9 AND $i = 3 Then $numTxt &= "Million "
        If $lenNum = 8 AND $i = 2 Then $numTxt &= "Million "
        If $lenNum = 7 AND $i = 1 Then $numTxt &= "Million "
        If Not($arrNum[$i] = 0) AND $lenNum = 9 AND $i = 6 Then $numTxt &= "Thousand "
        If Not($arrNum[$i] = 0) AND $lenNum = 8 AND $i = 5 Then $numTxt &= "Thousand "
        If Not($arrNum[$i] = 0) AND $lenNum = 7 AND $i = 4 Then $numTxt &= "Thousand "
        If $lenNum = 6 AND $i = 3 Then $numTxt &= "Thousand "
        If $lenNum = 5 AND $i = 2 Then $numTxt &= "Thousand "
        If $lenNum = 4 AND $i = 1 Then $numTxt &= "Thousand "

    Next

    Return $numTxt
EndFunc

Func _Ones($oNum)
    Select
        Case $oNum = 0
            Return ""
        Case $oNum = 1
            Return "One "
        Case $oNum = 2
            Return "Two "
        Case $oNum = 3
            Return "Three "
        Case $oNum = 4
            Return "Four "
        Case $oNum = 5
            Return "Five "
        Case $oNum = 6
            Return "Six "
        Case $oNum = 7
            Return "Seven "
        Case $oNum = 8
            Return "Eight "
        Case $oNum = 9
            Return "Nine "
    EndSelect
EndFunc

Func _Tens($tNum)
    Select
        Case $tNum = 0
            Return ""
        Case $tNum = 1
            Return "Ten "
        Case $tNum = 2
            Return "Twenty "
        Case $tNum = 3
            Return "Thirty "
        Case $tNum = 4
            Return "Fourty "
        Case $tNum = 5
            Return "Fifty "
        Case $tNum = 6
            Return "Sixty "
        Case $tNum = 7
            Return "Seventy "
        Case $tNum = 8
            Return "Eighty "
        Case $tNum = 9
            Return "Ninety "
    EndSelect
EndFunc

Does it not for you?

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

19 minutes ago, Melba23 said:

adityaparakh,

Use StringSplit to separate the 2 sections so you can run the code on each part

I will look at the 2 scripts now to see if I can find out what is wrong with them.

M23

 

Thanks , Trying to use 32.
It seems to be fine. However there is an issue.

 

it works with 16.2 but not with 16.02
Zero spoils it a bit. Can you please check.

 

Link to comment
Share on other sites

  • Moderators

adityaparakh,

Once you have split the numbers, just use StringLeft to see if there is a leading 0 and if so, use StringTrimLeft to remove it.

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

  • Moderators

adityaparakh,

Glad I could help.

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...