Tag Archives: format

String format (JavaScript)

String.prototype.format = function() { var a = arguments; return this.replace(/\{\d+\}/g, function©{ return a[c.match(/\d+/)]; }); }   // usage: var str = ‘Hello, {0}! You are {1}!’; str.format(‘friend’, ‘awesome’); // Hello, friend! You are awesome!String.prototype.format = function() { var a = arguments; return this.replace(/\{\d+\}/g, function©{ return a[c.match(/\d+/)]; }); } // usage: var str = ‘Hello, {0}! You […]

Bytes to Readable format (JavaScript)

function formatSize(v, c) { v = parseInt(v, 10); for(c = 0; v > 1024; c++) v = (v / 1024).toFixed(2); return v + ’ ’ + [‘B’, ‘kB’, ‘MB’, ‘GB’, ‘TB’, ‘PB’, ‘EB’, ‘ZB’, ‘YB’][c] }function formatSize(v, c) { v = parseInt(v, 10); for(c = 0; v > 1024; c++) v = (v / 1024).toFixed(2); […]