Created
January 19, 2012 01:40
-
-
Save vmi/1637055 to your computer and use it in GitHub Desktop.
simple string format utilities
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
/* | |
* 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