sshrum

How do I...do zero padding on a value

10 posts in this topic

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'

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

see StringFormat().

msgbox(4096,"",stringformat("%04d",1))
msgbox(4096,"",stringformat("%04d",100))
msgbox(4096,"",stringformat("%04d",1000))

Cheers

Kurt

Edited 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 *

Share this post


Link to post
Share on other sites

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'

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by robcull
changed name of function to be different than that of OP

Share this post


Link to post
Share on other sites

#6 ·  Posted

StringFormat() is already integrated, it has not changed in 11 years, and the code in post #2 still works   :yawn:

1 person likes this

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

On 8/7/2017 at 9:38 AM, mikell said:

...the code in post #2 still works   :yawn:

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 by robcull
i'm an idiot

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#9 ·  Posted

yes, see my edit on my last post :P

Share this post


Link to post
Share on other sites

#10 ·  Posted

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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