Permalink
Browse files

Fixes pull-request #263 - Add an extra argument to specify the number…

… of decimals for formatPercentage and formatCurrency
  • Loading branch information...
1 parent 1053a9a commit 6f815b9d13864d86ae1fc3d758a7c7ee2abc1fea @arian arian committed Jul 31, 2011
Showing with 20 additions and 7 deletions.
  1. +10 −2 Docs/Types/Number.Format.md
  2. +6 −4 Source/Types/Number.Format.js
  3. +4 −1 Tests/Specs/1.3/Types/Number.Format.js
@@ -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
@@ -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
@@ -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);
}
@@ -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(){
@@ -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(){

1 comment on commit 6f815b9

👍

Please sign in to comment.