From fcf13daf435366259fbc7734a05be071a4a62048 Mon Sep 17 00:00:00 2001 From: Sam Nicholls Date: Sun, 23 Dec 2012 15:01:07 +0000 Subject: [PATCH] numberFormat snippet --- snippets.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/snippets.js b/snippets.js index e82f506..55c7fec 100644 --- a/snippets.js +++ b/snippets.js @@ -253,3 +253,13 @@ RegExp.prototype.url_regex = function() { ); return reg; } + +Number.prototype.numberFormat = function(dec_places){ + //TODO Possibly abstract this to some sort of localisation module in future? + var dec_point = '.'; + var sep = ','; + + var parts = this.toFixed(dec_places).toString().split(dec_point); + parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, sep); + return parts.join(dec_point); +}