Skip to content

Instantly share code, notes, and snippets.

@namcoder
Created June 24, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namcoder/1fa2e3088b72cca36a9b58e3677bdcc9 to your computer and use it in GitHub Desktop.
Save namcoder/1fa2e3088b72cca36a9b58e3677bdcc9 to your computer and use it in GitHub Desktop.
Add Zero before number less than 10
('0' + 11).slice(-2) // '11'
('0' + 4).slice(-2) // '04'
For ease of access, you could of course extract it to a function, or even extend Number with it:
Number.prototype.pad = function(n) {
return new Array(n).join('0').slice((n || 2) * -1) + this;
}
Which will allow you to write:
c += deg.pad() + '° '; // "04° "
The above function pad accepts an argument specifying the length of the desired string. If no such argument is used, it defaults to 2. You could write:
deg.pad(4) // "0045"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment