Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 17th, 2007, 11:47 AM   #1
Silicon_Pudding
Newbie
 
Silicon_Pudding's Avatar
 
Join Date: Jun 2006
Location: In a box
Posts: 27
Rep Power: 0 Silicon_Pudding is on a distinguished road
Zero padding

How can I add zero pad a number based on a variable length so that the length of the number is always the same?
Example:
00004
00023
34243
Always has a length of 5 with zeros added in when necessary

Edit: nm i found out how using seq -w

anyways, i have another question about %04d . Instead of using 4 for the digits, how could I make 4 be a variable instead? Would %0$(length)d work instead?
__________________
~ Silicon Enhanced Pudding
"It's magically delicious"

Last edited by Silicon_Pudding; Feb 17th, 2007 at 12:25 PM.
Silicon_Pudding is offline   Reply With Quote
Old Feb 17th, 2007, 12:12 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just a suggestion: when you edit a post after finding a solution, please leave the original material. Other members may be looking for a solution to the same problem. Add your answer and help them out. Then add your new question, or start another thread.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 17th, 2007, 12:14 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
use printf %0nd $yourNum where n is the length you want displayed
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Feb 17th, 2007, 12:21 PM   #4
Silicon_Pudding
Newbie
 
Silicon_Pudding's Avatar
 
Join Date: Jun 2006
Location: In a box
Posts: 27
Rep Power: 0 Silicon_Pudding is on a distinguished road
Quote:
Originally Posted by Jimbo View Post
use printf %0nd $yourNum where n is the length you want displayed
is n a variable? I don't want n to be predefined, but calculated at run time.

I apologize for deleting the original post
__________________
~ Silicon Enhanced Pudding
"It's magically delicious"
Silicon_Pudding is offline   Reply With Quote
Old Feb 17th, 2007, 4:48 PM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
I don't know if there's a log function in bash, but if you know the length of the longest one you could use printf %0${maxlen}d $yourNum.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Feb 17th, 2007, 11:12 PM   #6
Silicon_Pudding
Newbie
 
Silicon_Pudding's Avatar
 
Join Date: Jun 2006
Location: In a box
Posts: 27
Rep Power: 0 Silicon_Pudding is on a distinguished road
Thanks for the help. Normally I would test it, but I'm borrowing a friend's comp so no Linux. Just trying to do the best without
__________________
~ Silicon Enhanced Pudding
"It's magically delicious"
Silicon_Pudding is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS Div Question brokenhope HTML / XHTML / CSS 8 Dec 21st, 2006 7:38 AM
A gap even though margin and padding are specified to zero. java_roshan HTML / XHTML / CSS 5 Aug 17th, 2006 1:05 AM
How can I get around Seg Faults metsfan C 19 Apr 24th, 2006 10:07 AM
Let's get this over and done with, once and for all [CSS enthusiasts this way] megamind5005 HTML / XHTML / CSS 16 Nov 10th, 2005 3:03 PM
MS CryptoAPI frustration - Cryptography SugarDaddy C++ 1 Jul 6th, 2005 3:05 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:30 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC