im just trying to add a style to the currency symbol. However, it looks weird when it is rendered on the page. And I want to change the style for one symbol only that looks weird.
1 Answer
Use $ sign in format()
numeral.register('locale', 'btc', {
delimiters: {
thousands: ',',
decimal: '.'
},
abbreviations: {
thousand: 'k',
million: 'm',
billion: 'b',
trillion: 't'
},
ordinal(number) {
const b = number % 10;
return (~~(number % 100 / 10) === 1) ? 'th' :
(b === 1) ? 'st' :
(b === 2) ? 'nd' :
(b === 3) ? 'rd' : 'th';
},
currency: {
symbol: '<span>฿</span>'
}
});
numeral.locale('btc');
var number = numeral(1000);
console.log(number.format('$0,0.00'))
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
But the recommended way of doing is custom-formats
numeral.register('locale', 'btc', {
delimiters: {
thousands: ',',
decimal: '.'
},
abbreviations: {
thousand: 'k',
million: 'm',
billion: 'b',
trillion: 't'
},
ordinal(number) {
const b = number % 10;
return (~~(number % 100 / 10) === 1) ? 'th' :
(b === 1) ? 'st' :
(b === 2) ? 'nd' :
(b === 3) ? 'rd' : 'th';
},
currency: {
symbol: '฿'
}
});
numeral.register('format', 'btc', {
regexps: {
format: /(BTC)/,
unformat: /(BTC)/
},
format: function(value, format, roundingFunction) {
var space = numeral._.includes(format, ' BTC') ? ' ' : '',
output;
value = value * 100;
// check for space before %
format = format.replace(/\s?\BTC/, '');
output = numeral._.numberToFormat(value, format, roundingFunction);
return '<span>฿</span>' + output;
},
unformat: function(string) {
return numeral._.stringToNumber(string) * 0.01;
}
});
numeral.locale('btc');
var number = numeral(1000);
console.log(number.format('$0'))
console.log(number.format('BTC0'))
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>