Skip to content

Commit

Permalink
Implement negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
neves committed Jul 20, 2017
1 parent f768db3 commit 042895a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ function format (input, opt = defaults) {
if (typeof input === 'number') {
input = input.toFixed(fixed(opt.precision))
}
var negative = input.indexOf('-') >= 0 ? '-' : ''

var numbers = onlyNumbers(input)
var currency = numbersToCurrency(numbers, opt.precision)
var parts = toStr(currency).split('.')
var integer = parts[0]
var decimal = parts[1]
integer = addThousandSeparator(integer, opt.thousands)
return opt.prefix + joinIntegerAndDecimal(integer, decimal, opt.decimal) + opt.suffix
return opt.prefix + negative + joinIntegerAndDecimal(integer, decimal, opt.decimal) + opt.suffix
}

function unformat (input, precision) {
var negative = input.indexOf('-') >= 0 ? -1 : 1
var numbers = onlyNumbers(input)
var currency = numbersToCurrency(numbers, precision)
return parseFloat(currency)
return parseFloat(currency) * negative
}

function onlyNumbers (input) {
Expand Down

0 comments on commit 042895a

Please sign in to comment.