sshrum Posted July 11, 2006 Share Posted July 11, 2006 I am writing a file renaming app and need a way to zero pad a number. I'd like to be able to pass a pad value and a number and have it return back the result; something like: _zeropad(4,1) -> 0001 _zeropad(4,100) -> 0100 _zeropad(4,10000) -> 10000 Anyone have something handy? TIA Sean Shrum :: http://www.shrum.net All my published AU3-based apps and utilities 'Make it idiot-proof, and someone will make a better idiot' Link to comment Share on other sites More sharing options...
/dev/null Posted July 11, 2006 Share Posted July 11, 2006 (edited) see StringFormat(). msgbox(4096,"",stringformat("%04d",1)) msgbox(4096,"",stringformat("%04d",100)) msgbox(4096,"",stringformat("%04d",1000)) Cheers Kurt Edited July 11, 2006 by /dev/null __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
sshrum Posted July 11, 2006 Author Share Posted July 11, 2006 NvrMnd....this is what I just whipped up: Func _ZeroPad($a, $b) $c = $a - StringLen($b) if $c > 0 Then for $d = $c to $a $b = "0" & $b Next EndIf return $b EndFunc Where $a is the pad and $b is the number to pad. Returns back the padded value. Sean Shrum :: http://www.shrum.net All my published AU3-based apps and utilities 'Make it idiot-proof, and someone will make a better idiot' Link to comment Share on other sites More sharing options...
sshrum Posted July 11, 2006 Author Share Posted July 11, 2006 StringFormat would work too. Thanks for the info. Sean Shrum :: http://www.shrum.net All my published AU3-based apps and utilities 'Make it idiot-proof, and someone will make a better idiot' Link to comment Share on other sites More sharing options...
robcull Posted August 7, 2017 Share Posted August 7, 2017 (edited) I know this thread is extremely old, but in case you're wondering, that script doesn't quite work- it pads one less 0 than it should (above, $a is desired stringlength after padding zeroes and $b is the input string, so (4, 1) should return "0001" but instead only returns "001". There is a much easier way to make this zeropad function. here it is: Func _ZeroPadII($n, $X) ;$n is desired string length after padded zeros, $X is input string while stringlen($X)<$n $X = "0" & $X wend return $X EndFunc Ex: _ZeroPadII(2, 1) = 01 _ZeroPadII(2, 10) = 10 _ZeroPadII(2, 100) = 100 _ZeroPadII(4, 2) = 0002 _ZeroPadII(4, 20) = 0020 _ZeroPadII(4, 200) = 0200 _ZeroPadII(4, 2000) = 2000 _ZeroPadII(4, 20000) = 20000 It'd be great if this could be added to the Int function. Edited August 16, 2017 by robcull changed name of function to be different than that of OP Link to comment Share on other sites More sharing options...
mikell Posted August 7, 2017 Share Posted August 7, 2017 StringFormat() is already integrated, it has not changed in 11 years, and the code in post #2 still works genius257 1 Link to comment Share on other sites More sharing options...
robcull Posted August 16, 2017 Share Posted August 16, 2017 (edited) On 8/7/2017 at 9:38 AM, mikell said: ...the code in post #2 still works No it doesn't. Look: ConsoleWrite(@CRLF&"_ZeroPad(3,1) = "& _ZeroPad(3,1) ) ConsoleWrite(@CRLF&"_ZeroPad(3,10) = "& _ZeroPad(3,10) ) ConsoleWrite(@CRLF&"_ZeroPad(3,10) = "& _ZeroPad(3,100) ) ConsoleWrite(@CRLF) ConsoleWrite(@CRLF&"_ZeroPadII(3,1) = "& _ZeroPadII(3,1) ) ConsoleWrite(@CRLF&"_ZeroPadII(3,10) = "& _ZeroPadII(3,10) ) ConsoleWrite(@CRLF&"_ZeroPadII(3,10) = "& _ZeroPadII(3,100) ) ConsoleWrite(@CRLF&@CRLF) Func _ZeroPad($a, $b) ;OP's code from post #2 $c = $a - StringLen($b) if $c > 0 Then for $d = $c to $a $b = "0" & $b Next EndIf return $b EndFunc Func _ZeroPadII($n, $X) ;my version while stringlen($X)<$n $X = "0" & $X wend return $X EndFunc Returns: _ZeroPad(3,1) = 001 _ZeroPad(3,10) = 00010 _ZeroPad(3,10) = 100 _ZeroPadII(3,1) = 001 _ZeroPadII(3,10) = 010 _ZeroPadII(3,10) = 100 EDIT: It just occurred to me that you were referring to the stringformat code in the actual second post, not the second post by the OP. Haha oops. yes, stringformat is great, i just wonder why they've never gotten around to updating the help file on it. It's very difficult to decipher for beginners, and the example provided leaves a lot to be wanting. I mean hell, the variable format is "%[flags] [width] [.precision] type" and yet the info on each of those parameters is written in the file in the order "width, type, flags, and finally precision." Edited August 16, 2017 by robcull i'm an idiot boomingranny 1 Link to comment Share on other sites More sharing options...
Developers Jos Posted August 16, 2017 Developers Share Posted August 16, 2017 (edited) 42 minutes ago, robcull said: No it doesn't. Look: post 2 isn't the code you show here but rather: On 11-7-2006 at 6:35 PM, /dev/null said: see StringFormat(). msgbox(4096,"",stringformat("%04d",1)) msgbox(4096,"",stringformat("%04d",100)) msgbox(4096,"",stringformat("%04d",1000)) Cheers Kurt Jos Edited August 16, 2017 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
robcull Posted August 16, 2017 Share Posted August 16, 2017 yes, see my edit on my last post Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 16, 2017 Moderators Share Posted August 16, 2017 robcull, Quote updating the help file on it. It's very difficult to decipher for beginners, and the example provided leaves a lot to be wanting. You should have seen that page before I rewrote it! Seriously, if you want to provide something that you think is better we would be happy to consider replacing the current content. M23 boomingranny 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area 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