Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Merge branch 'pr/121' into develop
Browse files Browse the repository at this point in the history
Conflicts:
	min/numeral.min.js
  • Loading branch information
adamwdraper committed Dec 25, 2013
2 parents c90d4d8 + e9d57a6 commit 9fa07e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
14 changes: 11 additions & 3 deletions numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@
}

function formatCurrency (n, format, roundingFunction) {
var prependSymbol = format.indexOf('$') <= 1 ? true : false,
var symbolIndex = format.indexOf('$'),
openParenIndex = format.indexOf('('),
minusSignIndex = format.indexOf('-'),
space = '',
spliceIndex,
output;

// check for space before or after currency
Expand All @@ -145,10 +148,15 @@
output = formatNumber(n._value, format, roundingFunction);

// position the symbol
if (prependSymbol) {
if (symbolIndex <= 1) {
if (output.indexOf('(') > -1 || output.indexOf('-') > -1) {
output = output.split('');
output.splice(1, 0, languages[currentLanguage].currency.symbol + space);
spliceIndex = 1;
if (symbolIndex < openParenIndex || symbolIndex < minusSignIndex){
// the symbol appears before the "(" or "-"
spliceIndex = 0;
}
output.splice(spliceIndex, 0, languages[currentLanguage].currency.symbol + space);
output = output.join('');
} else {
output = languages[currentLanguage].currency.symbol + space + output;
Expand Down
19 changes: 18 additions & 1 deletion tests/numeral/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,24 @@ exports.format = {
[-1000.234,'($0,0)','($1,000)'],
[-1000.234,'(0,0$)','(1,000$)'],
[-1000.234,'$0.00','-$1000.23'],
[1230974,'($0.00 a)','$1.23 m']
[1230974,'($0.00 a)','$1.23 m'],

// test symbol position before negative sign / open parens
[-1000.234,'$ (0,0)','$ (1,000)'],
[-1000.234,'$(0,0)','$(1,000)'],
[-1000.234,'$ (0,0.00)','$ (1,000.23)'],
[-1000.234,'$(0,0.00)','$(1,000.23)'],
[-1000.238,'$(0,0.00)','$(1,000.24)'],
[-1000.234,'$-0,0','$-1,000'],
[-1000.234,'$ -0,0','$ -1,000'],

[1000.234,'$ (0,0)','$ 1,000'],
[1000.234,'$(0,0)','$1,000'],
[1000.234,'$ (0,0.00)','$ 1,000.23'],
[1000.234,'$(0,0.00)','$1,000.23'],
[1000.238,'$(0,0.00)','$1,000.24'],
[1000.234,'$-0,0)','$1,000'],
[1000.234,'$ -0,0','$ 1,000']
],
i;

Expand Down

0 comments on commit 9fa07e3

Please sign in to comment.