Jump to content
Sign in to follow this  
Petert

Replacement for _StringAddThousandsSep()

Recommended Posts

Petert

Today I updated to the latest version of AutoIt. Soon I discovered that function _StringAddThousandsSep() no longer exists.

Is there a simple alternative way to add thousands separators to numbers (maybe with StringFormat)? I could write a small function that would do the trick but I like a one-line statement.

Share this post


Link to post
Share on other sites
KaFu

I just copied the old UDF to my scripts :idea:...

; #FUNCTION# ====================================================================================================================
; Name...........: _StringAddThousandsSepEx
; Description ...: Returns the original numbered string with the Thousands delimiter inserted.
; Syntax.........: _StringAddThousandsSep($sString[, $sThousands = -1[, $sDecimal = -1]])
; Parameters ....: $sString    - The string to be converted.
;                  $sThousands - Optional: The Thousands delimiter
;                  $sDecimal   - Optional: The decimal delimiter
; Return values .: Success - The string with Thousands delimiter added.
; Author ........: SmOke_N (orignal _StringAddComma
; Modified.......: Valik (complete re-write, new function name), KaFu (copied from 3.3.0.0, as function is deprecated in newer AU versions)
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _StringAddThousandsSepEx($sString, $sThousands = -1, $sDecimal = -1)
    Local $sResult = "" ; Force string
    Local $rKey = "HKCU\Control Panel\International"
    If $sDecimal = -1 Then $sDecimal = RegRead($rKey, "sDecimal")
    If $sThousands = -1 Then $sThousands = RegRead($rKey, "sThousand")
;~  Local $aNumber = StringRegExp($sString, "(\d+)\D?(\d*)", 1)
    Local $aNumber = StringRegExp($sString, "(\D?\d+)\D?(\d*)", 1) ; This one works for negatives.
    If UBound($aNumber) = 2 Then
        Local $sLeft = $aNumber[0]
        While StringLen($sLeft)
            $sResult = $sThousands & StringRight($sLeft, 3) & $sResult
            $sLeft = StringTrimRight($sLeft, 3)
        WEnd
;~      $sResult = StringTrimLeft($sResult, 1) ; Strip leading thousands separator
        $sResult = StringTrimLeft($sResult, StringLen($sThousands)) ; Strip leading thousands separator
        If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1]
    EndIf
    Return $sResult
EndFunc   ;==>_StringAddThousandsSepEx

Edit:

Btw, I love this animate gif :)...

Posted Image

Edited by KaFu

Share this post


Link to post
Share on other sites
Malkey

It appears the _StringAddThousandsSepEx function from the old String.au3 UDF include file is still among the worst versions of StringAddThousandsSep functions out there.

Try this one. It's a little better.

ConsoleWrite(_StringAddThousandsSep("61234567890.54321") & @CRLF)
ConsoleWrite(_StringAddThousandsSep("-$123123.50") & @CRLF)
ConsoleWrite(_StringAddThousandsSep("-8123.45") & @CRLF)
ConsoleWrite(_StringAddThousandsSep("-123.45") & @CRLF)
ConsoleWrite(_StringAddThousandsSep("-$25") & @CRLF)
ConsoleWrite(_StringAddThousandsSep("-$hello") & @CRLF)

; Instead of :-
; "Local $rKey = "HKCU\Control Panel\International"
;   If $sDecimal = -1 Then $sDecimal = RegRead($rKey, "sDecimal")
;   If $sThousands = -1 Then $sThousands = RegRead($rKey, "sThousand") ",
;
; $sThousands = ",", $sDecimal = "." are function parameters.
;
Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")
    Local $aNumber, $sLeft, $sResult = "", $iNegSign = "", $DolSgn = ""
    If Number(StringRegExpReplace($sString, "[^0-9\-.+]", "\1")) < 0 Then $iNegSign = "-" ; Allows for a negative value
    If StringRegExp($sString, "\$") And StringRegExpReplace($sString, "[^0-9]", "\1") <> "" Then $DolSgn = "$" ; Allow for Dollar sign
    $aNumber = StringRegExp($sString, "(\d+)\D?(\d*)", 1)
    If UBound($aNumber) = 2 Then
        $sLeft = $aNumber[0]
        While StringLen($sLeft)
            $sResult = $sThousands & StringRight($sLeft, 3) & $sResult
            $sLeft = StringTrimRight($sLeft, 3)
        WEnd
        $sResult = StringTrimLeft($sResult, 1); Strip leading thousands separator
        If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1] ; Add decimal
    EndIf
    Return $iNegSign & $DolSgn & $sResult ; Adds minus or "" (nothing)and Adds $ or ""
EndFunc ;==>_StringAddThousandsSep
Edited by Malkey
  • Like 1

Share this post


Link to post
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
Sign in to follow this  

×