diff --git a/accounting.js b/accounting.js index fd181fd..fe3ee62 100644 --- a/accounting.js +++ b/accounting.js @@ -156,6 +156,17 @@ zero : defaults }; + // If format is an object then neg and zero should be optional + } else if ( isObject( format ) && ( !format.neg || !format.zero ) ) { + + // If the negative or zero value is missing, populate based on positive + if ( !format.neg ) { + format.neg = format.pos.replace("%v", "-%v"); + } + if ( !format.zero ) { + format.zero = format.pos; + } + } // Otherwise, assume format was fine: return format; diff --git a/tests/qunit/methods.js b/tests/qunit/methods.js index 19e354b..4d56566 100644 --- a/tests/qunit/methods.js +++ b/tests/qunit/methods.js @@ -58,6 +58,7 @@ $(document).ready(function() { equals(accounting.formatMoney(0, { symbol: "GBP", format:format}), "GBP --", "`format` parameter provided given as an object with `zero` format, correctly observed in string output"); equals(accounting.formatMoney(-1000, { symbol: "GBP", format:format}), "GBP (1,000.00)", "`format` parameter provided given as an object with `neg` format, correctly observed in string output"); equals(accounting.formatMoney(1000, { symbol: "GBP", format:{neg:"--%v %s"}}), "GBP1,000.00", "`format` parameter provided, but only `neg` value provided - positive value should be formatted by default format (%s%v)"); + equals(accounting.formatMoney(-1000, { symbol: "GBP", format:{pos:"%s%v"}}), "GBP-1,000.00", "`format` parameter provided, but only `pos` value provided - negative and zero should be optional"); accounting.settings.currency.format = "%s%v"; accounting.formatMoney(0, {format:""});