Could be a bug in numeral.js. Regardless, if this is what you are attempting to do, you can drop your dependency for numeral.js and just use plain Javascript Math
and toLocaleString()
to deal with this.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
https://jsfiddle.net/qL41jg1x/
function toUSCurrency(x) {
var n = Math.round(x*100)/100
return n.toLocaleString(
'en-US' ,
{
style: 'currency' ,
currency: 'USD' ,
minimumFractionDigits: 2 ,
maximumFractionDigits: 2
}
) ;
}
document.getElementById("num1").innerHTML = toUSCurrency(-0.006);
document.getElementById("num2").innerHTML = toUSCurrency(-0.002);
document.getElementById("num3").innerHTML = toUSCurrency(-10000000.002);
document.getElementById("num4").innerHTML = toUSCurrency(10000000.002);
NOTES:
1) Math.round()
can get into the floating point nature of numbers if you have to deal with very large decimals.
2) toLocaleString()
seems to be supported by all current major browsers. https://caniuse.com/#feat=internationalization