Questions tagged [zero-pad]

41 questions
1108
votes
15 answers

How can I pad an integer with zeros on the left?

How do you left pad an int with zeros when converting to a String in java? I'm basically looking to pad out integers up to 9999 with leading zeros (e.g. 1 = 0001).
Omar Kooheji
  • 50,943
  • 65
  • 176
  • 234
137
votes
4 answers

Zero-pad digits in string

I need to cast single figures (1 to 9) to (01 to 09). I can think of a way but its big and ugly and cumbersome. I'm sure there must be some concise way. Any Suggestions
Giles
104
votes
6 answers

Python datetime formatting without zero-padding

Is there a format for printing Python datetimes that won't use zero-padding on dates and times? Format I'm using now: mydatetime.strftime('%m/%d/%Y %I:%M%p') Result: 02/29/2012 05:03PM Desired: 2/29/2012 5:03PM What format would represent the month…
Yarin
  • 144,097
  • 139
  • 361
  • 489
48
votes
6 answers

Zero pad numpy array

What's the more pythonic way to pad an array with zeros at the end? def pad(A, length): ... A = np.array([1,2,3,4,5]) pad(A, 8) # expected : [1,2,3,4,5,0,0,0] In my real use case, in fact I want to pad an array to the closest multiple of…
Basj
  • 29,668
  • 65
  • 241
  • 451
34
votes
10 answers

How can I count the digits in an integer without a string cast?

I fear there's a simple and obvious answer to this question. I need to determine how many digits wide a count of items is, so that I can pad each item number with the minimum number of leading zeros required to maintain alignment. For example, I…
Adam Siler
  • 1,908
  • 4
  • 21
  • 26
23
votes
4 answers

Formatting a date in R without leading zeros

Is there a way to use the format function on a date object, specifically an object of class POSIXlt, POSIXct, or Date, with the format %Y, %m, %d such that leading zeros are stripped from each of those 3 fields? For example, I would like…
Jon Claus
  • 2,543
  • 4
  • 17
  • 29
22
votes
6 answers

How to zero-pad numeric variables in zsh (and maybe also bash?)

In zsh, when I have to create a bunch of files with zsh, I usually do something like: for x in $(seq 1 1000); do .....; done This works fine, it gives me files with names foo-1.txt .. foo-1000.txt. However, these files do not sort nicely, so I…
ervingsb
  • 513
  • 6
  • 9
20
votes
2 answers

Java convert any integer to 4 digits

This seems like an easy question. One of my assignments basically sends a time in military format (like 1200, 2200, etc) to my class. How can I force the integer to be converted to 4 digits when it's received by my class? For example if the time…
Cody
  • 830
  • 3
  • 20
  • 37
17
votes
3 answers

C# Padding Amount With Zeros

I have an amount field which is a decimal in the database. I need to always display this amount with 10 numbers on the left of the decimal and two after. Example: Amount = 245.00 which should display as 0000000245.00 Additionally, the amount could…
Baxter
  • 5,033
  • 21
  • 64
  • 104
11
votes
4 answers

In PHP, how do I add to a zero-padded numeric string and preserve the zero padding?

If I have a variable in PHP containing 0001 and I add 1 to it, the result is 2 instead of 0002. How do I solve this problem?
Ryan
  • 331
  • 3
  • 8
  • 21
8
votes
1 answer

NSDateFormatter setDateFormat non-zero-padded month number

Is it possible to get the non-zero-padded month number using setDateFormat? If not, could you provide an alternative solution?
Tyilo
  • 25,959
  • 32
  • 101
  • 183
6
votes
2 answers

How to remove the boundary effects arising due to zero padding in scipy/numpy fft?

I have made a python code to smoothen a given signal using the Weierstrass transform, which is basically the convolution of a normalised gaussian with a signal. The code is as follows: #Importing relevant libraries from __future__ import…
Omkar
  • 163
  • 1
  • 4
5
votes
4 answers

Simple and elegant way to zero-pad a value in C#

I have to fix malformed zipcodes. The people who exported the data treated the zipcode as a numeric and as a result New Jersey and Puerto Rico zipcodes, which begin with a leading zero and two leading zeroes respectively, have been truncated.…
Tim
  • 8,256
  • 29
  • 96
  • 168
4
votes
1 answer

How to use boost::format to zeropad a number where the number of decimal places is contained in a variable?

I would like to zeropad a number such that it has 5 digits and get it as a string. This can be done with the following: unsigned int theNumber = 10; std::string theZeropaddedString = (boost::format("%05u") % theNumber).str(); However, I do not…
user3731622
  • 3,753
  • 2
  • 31
  • 64
3
votes
2 answers

Zero-padding a spinner in Java

How do you add zero padding to a JSpinner? Since the spinner creates the JFormattedTextField itself, I can't just pass the format into the JFormattedTextField constructor. Isn't there a way to set the formatting on an existing…
Melissa
  • 33
  • 5
1
2 3