Skip to content

Instantly share code, notes, and snippets.

@vmi
Created January 19, 2012 01:40
Show Gist options
  • Save vmi/1637055 to your computer and use it in GitHub Desktop.
Save vmi/1637055 to your computer and use it in GitHub Desktop.
simple string format utilities
/*
* Fair License (Fair)
*
* Copyright (c) 2012, IWAMURO Motonori
*
* Usage of the works is permitted provided that this instrument is
* retained with the works, so that any entity that uses the works is
* notified of this instrument.
*
* DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
*/
function comma(n) {
var s = String(n).split(".", 2);
var i = s[0].replace(/\B(?=(?:\d{3})+$)/g, ",");
return s.length == 1 ? i : i + "." + s[1];
}
function repeat(s, cnt) {
var r = "";
for (; cnt > 0; --cnt)
r += s;
return r;
}
function alignRight(s, col, pad) {
s = String(s);
// U+00A0 is NBSP.
return repeat(pad || "\u00a0", col - s.length) + s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment