Last active
August 29, 2015 14:00
-
-
Save adamretter/11361643 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(:~ | |
: Neat examples of zero-padding a one digit month, for later | |
: construction of xs:date etc. | |
:) | |
(: Example 1: When given a single digit month :) | |
let $month := '4' | |
return | |
substring(concat('0', $month), string-length($month)) | |
(: Example 2: When given a double digit month :) | |
let $month := '19' | |
return | |
substring(concat('0', $month), string-length($month)) | |
(: Example 3: suggested by @nine9ths 2014-04-28 :) | |
concat(('0')[string-length($month)], $month) | |
(: Example 4: Suggested by @michaelhkay 2014-04-28 :) | |
substring(string(100 + number($month), 2) |
these algorithms + a few more as a page in my cookbook http://kitwallace.co.uk/Book/set/zero-pad/page
The timings are all about the same and a better test here I think is how underatnable the code is, perhaps measured by quickly a reader can mentally verify that the code does what it intends to do. I should add voting to the website.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So far I like @michaelhkay's approach best