!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.numerify=n()}(this,function(){"use strict";var e={th:3,mi:6,bi:9,tr:12},n={zeroFormat:null,nullFormat:null,defaultFormat:"0,0",scalePercentBy100:!0,abbrLabel:{th:"k",mi:"m",bi:"b",tr:"t"}},t=1e12,r=1e9,i=1e6,l=1e3;function o(e,n,t,r){var i=e.toString().split("."),l=n-(r||0),o=2===i.length?Math.min(Math.max(i[1].length,l),n):l,a=Math.pow(10,o),f=(t(e+"e+"+o)/a).toFixed(o);if(r>n-o){var u=new RegExp("\\.?0{1,"+(r-(n-o))+"}$");f=f.replace(u,"")}return f}function a(n,a,f,u){var c=Math.abs(a),d=!1,m=!1,p="",s="",b=!1,g=void 0,x=void 0;f=f||"",a=a||0,~f.indexOf("(")?(d=!0,f=f.replace(/[(|)]/g,"")):(~f.indexOf("+")||~f.indexOf("-"))&&(x=~f.indexOf("+")?f.indexOf("+"):a<0?f.indexOf("-"):-1,f=f.replace(/[+|-]/g,"")),~f.indexOf("a")&&(g=!!(g=f.match(/a(k|m|b|t)?/))&&g[1],~f.indexOf(" a")&&(p=" "),f=f.replace(new RegExp(p+"a[kmbt]?"),""),c>=t&&!g||"t"===g?(p+=n.abbrLabel.tr,a/=t):c=r&&!g||"b"===g?(p+=n.abbrLabel.bi,a/=r):c=i&&!g||"m"===g?(p+=n.abbrLabel.mi,a/=i):(c=l&&!g||"k"===g)&&(p+=n.abbrLabel.th,a/=l)),~f.indexOf("[.]")&&(m=!0,f=f.replace("[.]","."));var h=a.toString().split(".")[0],O=f.split(".")[1],v=f.indexOf(","),F=(f.split(".")[0].split(",")[0].match(/0/g)||[]).length;if(O?(h=(s=~O.indexOf("[")?o(a,(O=(O=O.replace("]","")).split("["))[0].length+O[1].length,u,O[1].length):o(a,O.length,u)).split(".")[0],s=~s.indexOf(".")?"."+s.split(".")[1]:"",m&&0==+s.slice(1)&&(s="")):h=o(a,0,u),p&&!g&&+h>=1e3&&p!==e.trillion&&(h=""+ +h/1e3,p=e.million),~h.indexOf("-")&&(h=h.slice(1),b=!0),h.length0;y--)h="0"+h;v>-1&&(h=h.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1,")),f.indexOf(".")||(h="");var k=h+s+(p||"");return d?k=(d&&b?"(":"")+k+(d&&b?")":""):x>=0?k=0===x?(b?"-":"+")+k:k+(b?"-":"+"):b&&(k="-"+k),k}function f(e,n){Object.keys(n).forEach(function(t){e[t]=n[t]})}var u={},c={};function d(e,n,t){return function(e,n,t){n=n||u.defaultFormat,t=t||Math.round;var r=void 0,i=void 0;if(0===e&&null!==u.zeroFormat)r=u.zeroFormat;else if(null===e&&null!==u.nullFormat)r=u.nullFormat;else{for(var l in c)if(c[l]&&n.match(c[l].regexp)){i=c[l].format;break}r=(i=i||a.bind(null,u))(e,n,t,d)}return r}(0===e||void 0===e?0:null===e||function(e){return"number"==typeof e&&isNaN(e)}(e)?null:"string"==typeof e?u.zeroFormat&&e===u.zeroFormat?0:u.nullFormat&&e===u.nullFormat||!e.replace(/[^0-9]+/g,"").length?null:+e:+e||null,n,t)}return f(u,n),d.options=u,d._numberToFormat=a.bind(null,u),d.register=function(e,n){c[e]=n},d.unregister=function(e){c[e]=null},d.setOptions=function(e){f(u,e)},d.reset=function(){f(u,n)},d.register("percentage",{regexp:/%/,format:function(e,n,t,r){var i=~n.indexOf(" %")?" ":"",l=void 0;return r.options.scalePercentBy100&&(e*=100),n=n.replace(/\s?%/,""),~(l=r._numberToFormat(e,n,t)).indexOf(")")?((l=l.split("")).splice(-1,0,i+"%"),l=l.join("")):l=l+i+"%",l}}),d});