leuce Posted October 19, 2019 Share Posted October 19, 2019 Hello everyone I saw in previous messages that one can use StringFormat to add leading zeros so a number so that the "number" (i.e. the string with leading zeros) always has a specific number of characters, but I'm afraid I don't understand how it works. Can anyone here please tell me how? At the moment, if I want to have leading zeros, I use something like this: For $i = 1 to 100 $j = $i If $i < 100000 Then $j = "0" & $j EndIf If $i < 10000 Then $j = "0" & $j EndIf If $i < 1000 Then $j = "0" & $j EndIf If $i < 100 Then $j = "0" & $j EndIf If $i < 10 Then $j = "0" & $j EndIf Next I use these numbers e.g. when creating file names and I want them to sort alphabetically correctly. Thanks. Samuel Link to comment Share on other sites More sharing options...
water Posted October 19, 2019 Share Posted October 19, 2019 (edited) This way? For $i = 1 To 100 $j = $i If $i < 100000 Then $j = "0" & $j EndIf If $i < 10000 Then $j = "0" & $j EndIf If $i < 1000 Then $j = "0" & $j EndIf If $i < 100 Then $j = "0" & $j EndIf If $i < 10 Then $j = "0" & $j EndIf ConsoleWrite($j & ", StringFormat: " & StringFormat("%06i", $i) & @CRLF) Next "%06i" can be interpreted as. "%" is a literal, flag "0" for leading zeroes, width "6" means result is 6 characters wide, type "i" stands for signed integer. Edited October 19, 2019 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Exit Posted October 19, 2019 Share Posted October 19, 2019 Or this way? 😉 For $i=1 To 100 ConsoleWrite(StringTrimLeft(1000000+$i,1) & @CRLF) Next App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
BigDaddyO Posted October 22, 2019 Share Posted October 22, 2019 I typically do something like this. $sPadding = "0000000" ;Fixed length of your value $Value = 43 ;Length of the number you got Consolewrite(StringRight($sPadding & $Value, 7) & @crlf) Link to comment Share on other sites More sharing options...
jchd Posted October 22, 2019 Share Posted October 22, 2019 Fails with negative values. StringFormat is more robust and flexible. 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
leuce Posted October 26, 2019 Author Share Posted October 26, 2019 Thanks, everyone! Link to comment Share on other sites More sharing options...
TheSaint Posted October 26, 2019 Share Posted October 26, 2019 (edited) Something like what BigDaddyO suggested if you are dealing with a known fixed length. I regularly use a variation of the following. $num = StringRight("000" & $num, 4) In that scenario, the initial source $num can be 1 to 1000 and you always get the correct 4 digit number with required leading zeroes. EDIT - That 1000 might be misleading, as it could be as much as 9999 .... I was just illustrating in basic terms of 1 to 4 digits. Edited October 26, 2019 by TheSaint Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
iamtheky Posted October 26, 2019 Share Posted October 26, 2019 (edited) looks like a fun thought experiment 'if stringformat did not exist', this one seems to play nice with negatives and existing leading zeroes: #include<string.au3> $n = -0066 $length = 7 msgbox(0, '' , (number($n) < 0 ? "-" : "") & _StringRepeat(0 , $length - stringlen(Abs($n))) & Abs($n)) Edited October 26, 2019 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Deye Posted October 26, 2019 Share Posted October 26, 2019 @stretching Gum #include <String.au3> MsgBox(0, '', IL(43, 6)) Func IL($Value = "", $iCount = 5, $lChar = 0) Return (Abs($Value) = $Value ? "" : "-") & _StringRepeat($lChar, $iCount - StringLen(Abs($Value))) & Abs($Value) EndFunc ;==>InsertLeading Link to comment Share on other sites More sharing options...
mikell Posted October 28, 2019 Share Posted October 28, 2019 On 10/26/2019 at 8:09 PM, iamtheky said: looks like a fun thought experiment ... and - of course - the regex version #include<string.au3> $n = -0066 $length = 7 $r = Execute(StringRegExpReplace($n, '(-?)0*(' & Abs($n) & ')', _ "'$1' & _StringRepeat(0, $length - stringlen('$2')) & '$2' & ") & "''") msgbox(0, '' , $r) Link to comment Share on other sites More sharing options...
TheSaint Posted October 28, 2019 Share Posted October 28, 2019 (edited) LOL. I love it when code gets taken to the extremes. About time we had some more code contests. 1. The simplest way to do something. 2. The most complex way. 3. The cleverest. 4. The dumbest. P.S. No doubt a good learning experience on occasion. Edited October 28, 2019 by TheSaint FrancescoDiMuro 1 Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted October 28, 2019 Share Posted October 28, 2019 @TheSaint When another one of those? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Gianni Posted October 28, 2019 Share Posted October 28, 2019 ... just for fun, a version that does not use StringFormat() nor #include <string.au3> nor regex $nr = -0066 $length = 7 MsgBox(0, '', Num_Len($nr, $length)) Func Num_Len($n, $length) Local $a[$length] Return (Number($n) < 0 ? "-" : "") & StringRight(StringReplace(StringFromASCIIArray($a), Chr(0), '0') & Abs($n), $length) EndFunc ;==>Num_Len Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
TheSaint Posted October 29, 2019 Share Posted October 29, 2019 10 hours ago, FrancescoDiMuro said: @TheSaint When another one of those? @czardas - was the mover and shaker behind them. If you are keen for some more, maybe let him know. FrancescoDiMuro 1 Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
Deye Posted October 30, 2019 Share Posted October 30, 2019 (edited) Back to innocence example #include <string.au3> MsgBox(0, '', _ARLeadingZeros(-00066, True) & @CRLF & _ARLeadingZeros(-00066, False, 3)) Func _ARLeadingZeros($n, $bRemove = False, $length = 6) If $bRemove Or StringLen(Abs($n)) >= $length Then Return $n Local $nP = 1 & _StringRepeat(0, $length), $nN = "-" & StringTrimLeft($nP - $n, 1) Return $nN = $n ? $nN : StringTrimLeft($nP + $n, 1) EndFunc ;==>_AddRemoveLeadingZeros Edited October 30, 2019 by Deye TheSaint 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