zero-pad

Formatting a date in R without leading zeros

丶灬走出姿态 提交于 2019-11-28 07:29:34
问题 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 format(as.Date("1998-09-02"), "%Y, %m, %d") to return 1998, 9, 2 and not 1998, 09, 02 . 回答1: Just remove the leading zeros at the end: > gsub(" 0", " ", format(as.Date("1998-09-02"), "%Y, %m, %d")) [1] "1998, 9, 2" Use %e to obtain a leading space instead

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

情到浓时终转凉″ 提交于 2019-11-27 12:38:07
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 want no leading zeros if the total is < 10, 1 if it's between 10 and 99, etc. One solution would be to cast the item count to a string and then count characters. Yuck! Is there a better way? Edit: I would not have thought to use the common logarithm (I didn't know such a thing existed). So, not obvious - to me - but definitely simple. Nicholas Mancuso

Python datetime formatting without zero-padding

佐手、 提交于 2019-11-27 11:11:09
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 as '2' instead of '02', and time as '5:03PM' instead of '05:03PM' Sven Marnach The formatting options available with datetime.strftime() will all zero-pad. You could of course roll you own formatting function, but the easiest solution in this case might be to post-process the result of datetime.strftime() : s = mydatetime.strftime('%m/%d/%Y %I:%M%p'

Zero pad numpy array

谁说我不能喝 提交于 2019-11-27 02:04:38
问题 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 1024. Ex: 1342 => 2048, 3000 => 3072 回答1: 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

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

雨燕双飞 提交于 2019-11-26 16:05:27
问题 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 want no leading zeros if the total is < 10, 1 if it's between 10 and 99, etc. One solution would be to cast the item count to a string and then count characters. Yuck! Is there a better way? Edit: I would not have thought to use the common logarithm

Zero-pad digits in string

情到浓时终转凉″ 提交于 2019-11-26 12:59:54
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 Konrad Rudolph First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that: $s = sprintf('%02d', $digit); For more information, refer to the documentation of sprintf . There's also str_pad <?php $input = "Alien"; echo str_pad($input, 10); // produces "Alien " echo str_pad($input, 10, "-=", STR_PAD_LEFT); /

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

僤鯓⒐⒋嵵緔 提交于 2019-11-25 21:45:20
问题 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 ). 回答1: 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. 回答2: If you for any reason use pre 1.5 Java then may try