Questions tagged [zero-pad]

41 questions
1212
votes
19 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
  • 54,530
  • 68
  • 182
  • 238
139
votes
9 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
  • 173,523
  • 149
  • 402
  • 512
71
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
  • 41,386
  • 99
  • 383
  • 673
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,986
  • 5
  • 22
  • 26
33
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
  • 653
  • 8
  • 9
31
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,862
  • 4
  • 22
  • 33
23
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
  • 870
  • 4
  • 21
  • 38
19
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,633
  • 24
  • 69
  • 105
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
  • 341
  • 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
  • 28,998
  • 40
  • 113
  • 198
6
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,669
  • 31
  • 105
  • 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
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
  • 4,844
  • 8
  • 45
  • 84
4
votes
1 answer

Is it possible to pad leading zeros with a space instead of a zero in C?

Wondering if I did something like printf("%04.2f", float_variable); Could I have it print " 1.15" Instead of "01.15" Weird request I know, but it is what I need. No idea how to start.
Zack
  • 13,454
  • 24
  • 75
  • 113
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