Skip to content

Instantly share code, notes, and snippets.

@adamretter
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamretter/11361643 to your computer and use it in GitHub Desktop.
Save adamretter/11361643 to your computer and use it in GitHub Desktop.
(:~
: 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)
@adamretter
Copy link
Author

So far I like @michaelhkay's approach best

@KitWallace
Copy link

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