Petert Posted April 23, 2010 Share Posted April 23, 2010 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. Link to comment Share on other sites More sharing options...
KaFu Posted April 23, 2010 Share Posted April 23, 2010 (edited) I just copied the old UDF to my scripts ...; #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 ;==>_StringAddThousandsSepExEdit:Btw, I love this animate gif ... Edited April 23, 2010 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Malkey Posted April 23, 2010 Share Posted April 23, 2010 (edited) 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 April 23, 2010 by Malkey Skysnake 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now