0

我使用numeric.js

我对自定义格式感兴趣, 只要有负数(比如 - 20),图书馆就会向我显示它,所以 20 (-)

我需要将减号放在括号中

我怎样才能做到这一点?

4

1 回答 1

1

你可以做类似的事情

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>

于 2020-08-20T10:46:40.907 回答