zero-pad
How can I count the digits in an integer without a string cast?
This should do it: int length = (number ==0) ? 1 : (int)Math.log10(number) + 1;
Zero pad numpy array
numpy.pad with constant mode does what you need, where we can pass a tuple as second argument to tell how many zeros to pad on each size, a (2, 3) for instance will pad 2 zeros on the left side and 3 zeros on the right side: Given A as: A = np.array([1,2,3,4,5]) np.pad(A, (2, … Read more
Python datetime formatting without zero-padding
The other alternate to avoid the “all or none” leading zero aspect above is to place a minus in front of the field type: mydatetime.strftime(‘%-m/%d/%Y %-I:%M%p’) Then this: ‘4/10/2015 03:00AM’ Becomes: ‘4/10/2015 3:00AM’ You can optionally place a minus in front of the day if desired. Edit: The minus feature derives from the GNU C … Read more
How can I pad an integer with zeros on the left?
Use java.lang.String.format(String,Object…) like this: String.format(“%05d”, yournumber); for zero-padding with a length of 5. For hexadecimal output replace the d with an x as in “%05x”. The full formatting options are documented as part of java.util.Formatter.