我使用numeral.js库
我对Do custom format感兴趣,只要有负数(比如- 20),图书馆就会把它显示给我,所以是20 (-)
我需要把减号放在括号里
我该怎么做呢?
发布于 2020-08-20 18:46:41
你可以做类似这样的事情
numeral.register('format', 'negative', {
regexps: {
format: /(-)/,
unformat: /(-)/
},
format: function(value, format, roundingFunction) {
return value < 0 ? -value + " (-)" : value;
},
unformat: function(s) {
return s.endsWith(" (-)") ? -parseInt(s.substr(0, s.length - 4)) : parseInt(s);
}
});
// use your custom format
const result = numeral(-5).format('(-)');
const unresult = numeral(result).format();
console.log(result)
console.log(unresult)<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
https://stackoverflow.com/questions/63503163
复制相似问题