Created
September 10, 2013 04:19
-
-
Save wilhelm-murdoch/6504925 to your computer and use it in GitHub Desktop.
A better variation of AngularJS' ngNumber 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
/** | |
* Number Filter | |
* @Param input The number to format | |
* @Param fractionSize Nubmer of decimal places to round to; defaults to 2 | |
* @Param groupSep Number group separator; defaults to a single space ' ' | |
* @Param decimalSep Decimal place separator; defaults to single period | |
* @return string, ex: 1 234 345 | |
*/ | |
var DECIMAL_SEP = '.' // change to override default | |
var GROUP_SEP = ' ' // change to override default | |
localmeasure.filter('number', ['$locale', | |
function ($locale) { | |
return function (number, fractionSize, groupSep, decimalSep) { | |
var pattern = $locale.NUMBER_FORMATS.PATTERNS[0] | |
if(!groupSep) { | |
groupSep = GROUP_SEP | |
} | |
if(!decimalSep) { | |
decimalSep = DECIMAL_SEP | |
} | |
if (isNaN(number) || !isFinite(number)) return ''; | |
var isNegative = number < 0; | |
number = Math.abs(number); | |
var numStr = number + '', | |
formatedText = '', | |
parts = []; | |
var hasExponent = false; | |
if (numStr.indexOf('e') !== -1) { | |
var match = numStr.match(/([\d\.]+)e(-?)(\d+)/); | |
if (match && match[2] == '-' && match[3] > fractionSize + 1) { | |
numStr = '0'; | |
} else { | |
formatedText = numStr; | |
hasExponent = true; | |
} | |
} | |
if (!hasExponent) { | |
var fractionLen = (numStr.split(DECIMAL_SEP)[1] || '').length; | |
// determine fractionSize if it is not specified | |
if (angular.isUndefined(fractionSize)) { | |
fractionSize = Math.min(Math.max(pattern.minFrac, fractionLen), pattern.maxFrac); | |
} | |
var pow = Math.pow(10, fractionSize); | |
number = Math.round(number * pow) / pow; | |
var fraction = ('' + number).split(DECIMAL_SEP); | |
var whole = fraction[0]; | |
fraction = fraction[1] || ''; | |
var pos = 0, | |
lgroup = pattern.lgSize, | |
group = pattern.gSize; | |
if (whole.length >= (lgroup + group)) { | |
pos = whole.length - lgroup; | |
for (var i = 0; i < pos; i++) { | |
if ((pos - i) % group === 0 && i !== 0) { | |
formatedText += groupSep; | |
} | |
formatedText += whole.charAt(i); | |
} | |
} | |
for (i = pos; i < whole.length; i++) { | |
if ((whole.length - i) % lgroup === 0 && i !== 0) { | |
formatedText += groupSep; | |
} | |
formatedText += whole.charAt(i); | |
} | |
// format fraction part. | |
while (fraction.length < fractionSize) { | |
fraction += '0'; | |
} | |
if (fractionSize && fractionSize !== "0") formatedText += decimalSep + fraction.substr(0, fractionSize); | |
} else { | |
if (fractionSize > 0 && number > -1 && number < 1) { | |
formatedText = number.toFixed(fractionSize); | |
} | |
} | |
parts.push(isNegative ? pattern.negPre : pattern.posPre); | |
parts.push(formatedText); | |
parts.push(isNegative ? pattern.negSuf : pattern.posSuf); | |
return parts.join(''); | |
} | |
} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks interesting. How to use this to override the built-in number filter?