Skip to content

Commit

Permalink
Fixes pull-request #263 - Add an extra argument to specify the number…
Browse files Browse the repository at this point in the history
… of decimals for formatPercentage and formatCurrency
  • Loading branch information
Arian committed Jul 31, 2011
1 parent 1053a9a commit 6f815b9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
12 changes: 10 additions & 2 deletions Docs/Types/Number.Format.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ Formats a number as currency, with respect to localization.

### Syntax

myNumber.formatCurrency();
myNumber.formatCurrency(decimals);

### Arguments

1. decimals - (*number*) the number of decimals

### Example

Expand All @@ -76,7 +80,11 @@ Formats a number as a percentage.

### Syntax

myNumber.formatPercentage();
myNumber.formatPercentage(decimals);

### Arguments

1. decimals - (*number*) the number of decimals

### Example

Expand Down
10 changes: 6 additions & 4 deletions Source/Types/Number.Format.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,20 @@ Number.implement({
return value;
},

formatCurrency: function(){
formatCurrency: function(decimals){
var locale = Locale.get('Number.currency') || {};
if (locale.scientific == null) locale.scientific = false;
if (locale.decimals == null) locale.decimals = 2;
locale.decimals = decimals != null ? decimals
: (locale.decimals == null ? 2 : locale.decimals);

return this.format(locale);
},

formatPercentage: function(){
formatPercentage: function(decimals){
var locale = Locale.get('Number.percentage') || {};
if (locale.suffix == null) locale.suffix = '%';
if (locale.decimals == null) locale.decimals = 2;
locale.decimals = decimals != null ? decimals
: (locale.decimals == null ? 2 : locale.decimals);

return this.format(locale);
}
Expand Down
5 changes: 4 additions & 1 deletion Tests/Specs/1.3/Types/Number.Format.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('Number.Format', function(){

it('should format a currency', function(){
expect((2000).formatCurrency()).toEqual('$ 2,000.00');
expect((2000).formatCurrency(0)).toEqual('$ 2,000');
});

it('should format a negative currency', function(){
Expand All @@ -73,7 +74,9 @@ describe('Number.Format', function(){
});

it('should format percentage', function(){
expect((50).formatPercentage()).toEqual('50.00%');
expect((50.123).formatPercentage()).toEqual('50.12%');
expect((50.123).formatPercentage(1)).toEqual('50.1%');
expect((50.123).formatPercentage(0)).toEqual('50%');
});

it('should not change the options object', function(){
Expand Down

1 comment on commit 6f815b9

@philfreo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.