0218fde3a78fadbadb566bdb40d7b0dd

This is pretty much a port of the Ruby on Rails number_to_currency method, right down to the hashed options.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 !

90dfcf7e54299842383ab503b6df1a65

Ali Karbassi

October 21, 2007, October 21, 2007 23:57, permalink

No rating. Login to rate!

Much easier way of doing this.

1
2
3
4
5
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) : '');
};
0218fde3a78fadbadb566bdb40d7b0dd

Barry Hess

October 22, 2007, October 22, 2007 02:07, permalink

No rating. Login to rate!

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.

E64f8a0dfd37fe636ce178d4d538e327

Alfred

December 17, 2007, December 17, 2007 15:54, permalink

No rating. Login to rate!

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.

C97ded8e1c6497f79b43a3413edf77e8

pink

July 17, 2008, July 17, 2008 13:43, permalink

No rating. Login to rate!

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.

0218fde3a78fadbadb566bdb40d7b0dd

Barry Hess

July 17, 2008, July 17, 2008 14:39, permalink

No rating. Login to rate!

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.

5071c5b861341c0dcfcf6ac86327701f

Tien Dung

July 23, 2008, July 23, 2008 04:12, permalink

No rating. Login to rate!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}
5071c5b861341c0dcfcf6ac86327701f

Tien Dung

July 23, 2008, July 23, 2008 04:24, permalink

No rating. Login to rate!

Oops! I forgot precision = 0 case. Here are the update version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 : "");
}

Your refactoring





Format Copy from initial code

or Cancel