0

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
  • How are you binding this to html? Please provide your html template file code. Commented May 25, 2021 at 16:14

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.