Created
May 18, 2017 02:56
-
-
Save shimar/ca8aea90d4902f0628548a97a6b36f73 to your computer and use it in GitHub Desktop.
Currency Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
/** | |
* 12345 => $12,345.00 | |
* | |
* @param {String} sign | |
* @param {Number} decimals Decimal places | |
*/ | |
currency (value, currency, decimals) { | |
value = parseFloat(value) | |
if (!isFinite(value) || (!value && value !== 0)) return '' | |
currency = currency != null ? currency : '$' | |
decimals = decimals != null ? decimals : 2 | |
var stringified = Math.abs(value).toFixed(decimals) | |
var _int = decimals | |
? stringified.slice(0, -1 - decimals) | |
: stringified | |
var i = _int.length % 3 | |
var head = i > 0 | |
? (_int.slice(0, i) + (_int.length > 3 ? ',' : '')) | |
: '' | |
var _float = decimals | |
? stringified.slice(-1 - decimals) | |
: '' | |
var sign = value < 0 ? '-' : '' | |
return sign + currency + head + | |
_int.slice(i).replace(digitsRE, '$1,') + | |
_float | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment