number_to_currency: function (number, options) {
try {
var options = options || {};
var precision = options["precision"] || 2;
var unit = options["unit"] || "$";
var separator = precision > 0 ? options["separator"] || "." : "";
var delimiter = options["delimiter"] || ",";
var parts = parseFloat(number).toFixed(precision).split('.');
return unit + reports.number_with_delimiter(parts[0], delimiter) + separator + parts[1].toString();
} catch(e) {
return number
}
},
number_with_delimiter: function (number, delimiter, separator) {
try {
var delimiter = delimiter || ",";
var separator = separator || ".";
var parts = number.toString().split('.');
parts[0] = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + delimiter);
return parts.join(separator);
} catch(e) {
return number
}
}
Refactorings
No refactoring yet !
Ali Karbassi
October 21, 2007, October 21, 2007 23:57, permalink
Much easier way of doing this.
Number.prototype.toCurrency = function(floatPoint, decimalSep, thousandsSep, unit)
{
var n = this, c = isNaN(c = Math.abs(floatPoint)) ? 2 : c, d = decimalSep == undefined ? "." : decimalSep, t = thousandsSep == undefined ? "." : thousandsSep, i = parseInt(n = (+n || 0).toFixed(c)) + '', j = (j = i.length) > 3 ? j % 3 : 0;
return (unit == undefined ? '$': unit) + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + (n - i).toFixed(c).slice(2) : '');
};
Barry Hess
October 22, 2007, October 22, 2007 02:07, permalink
Thanks, Ali. So am I the only unusual being who doesn't find Ali's refactor to be "much easier?" I don't know - I find the original more readable.
Alfred
December 17, 2007, December 17, 2007 15:54, permalink
I also think Barry Hess codes is more readable then Ali's. Some people think if you put a lot of code on 1 line that it is more readable, but i don't think so.
pink
July 17, 2008, July 17, 2008 13:43, permalink
hi, how do you use this? I tried using it, but I guess the default number_to_currency is still being used. I tried renaming it, but it could not be found. I included it correctly.
Barry Hess
July 17, 2008, July 17, 2008 14:39, permalink
This is JavaScript - I don't think JS or Prototype _has_ a number_to_currency method. At least not when I wrote this. You may need to look at some syntax things - I ripped this out of a JS file, and there could be some cruft that doesn't belong or syntax may be different in your application.
Tien Dung
July 23, 2008, July 23, 2008 04:12, permalink
I combine two function in one and make it look more like JavaScript.
Notice that I use truncating the fractionPart instead of using float.toFixed().
You may argue that numberToCurrency(99.999) will produce $99.99 instead of $100.00 but with this case
numberToCurrency(99.99, {precision: 15}) when using toFixed() you will get ""$99.989999999999995" intead of "$99.990000000000000". That's the pitfall of toFixed() in JavaScript.
function numberToCurrency(number, format) {
var match, defaultFormat, property, integerPart, fractionalPart;
match = number.toString().match(/([\+\-]?[0-9]*)(.[0-9]+)?/);
if (!match) return;
defaultFormat = { precision:2, unit: "$", separator: ".", delimiter : "," };
format = format || {};
for (property in defaultFormat)
format[property] = format[property] || defaultFormat[property];
integerPart = match[1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + format.delimiter);
fractionalPart = (match[2].toString() + "000000000000").substr(1, format.precision);
return format.unit + integerPart + format.separator + fractionalPart;
}
Tien Dung
July 23, 2008, July 23, 2008 04:24, permalink
Oops! I forgot precision = 0 case. Here are the update version
function numberToCurrency(number, format) {
var match, defaultFormat, property, integerPart, fractionalPart;
match = number.toString().match(/([\+\-]?[0-9]*)(.[0-9]+)?/);
if (!match) return;
defaultFormat = { precision: 2, unit: "$", separator: ".", delimiter : "," };
format = format || {};
for (property in defaultFormat)
if (typeof format[property] === "undefined")
format[property] = defaultFormat[property];
integerPart = match[1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + format.delimiter);
fractionalPart = (match[2].toString() + "000000000000").substr(1, format.precision);
return format.unit + integerPart + ( format.precision > 0 ? format.separator + fractionalPart : "");
}
Peter Hollows
June 8, 2009, June 08, 2009 04:52, permalink
Thanks for the suggestions everyone. Here's one that works given the amount in cents and simple.
// Function:
Number.prototype.toCurrency = function() {
var match, defaultFormat, property, integerPart, fractionalPart;
match = (this / 100).toString().match(/^(\d+)(\.\d+)?$/);
if (!match) return;
cents = match[2] ? (match[2] + '000').substr(1, 2) : '00';
dollars = match[1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
return '$' + dollars + '.' + cents;
};
// Example:
4433260.toCurrency() == '$44,332.60';
Dojo7 Rails development
June 8, 2009, June 08, 2009 07:04, permalink
Right, newer version that supports negative numbers and has other fixes.
Number.prototype.toCurrency = function() {
var match, cents, dollars, result;
match = (this / 100).toString().match(/^-?(\d+)(\.\d+)?$/);
if (!match) return;
cents = match[2] ? (match[2] + '00').substr(1, 2) : '00';
dollars = match[1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
result = '$' + dollars + '.' + cents;
if (this < 0) { result = '-' + result };
return result;
};
Josh Starcher
January 20, 2011, January 20, 2011 19:47, permalink
For the jquery users out there
$.fn.numberToCurrency = function(number, options) {
var match, property, integerPart, fractionalPart;
var settings = $.extend({precision: 2, unit: "$", separator: ".", delimiter : ","}, options || {});
match = number.toString().match(/([\+\-]?[0-9]*)(.[0-9]+)?/);
if (!match) return;
integerPart = match[1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + settings.delimiter);
fractionalPart = match[2] ? (match[2].toString() + "000000000000").substr(1, settings.precision) : "000000000000".substr(1, settings.precision);
return settings.unit + integerPart + ( settings.precision > 0 ? settings.separator + fractionalPart : "");
}
This is pretty much a port of the Ruby on Rails number_to_currency method, right down to the hashed options.