1

We need to format number only k(thousand) not m(million) or b(billion) with numeraljs currently its converting all formats

var number = 2000000; 
console.log(numeral(number).format('0a'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>

Expected result id 2000K Thanks

Govind Samrow
  • 9,981
  • 13
  • 53
  • 90

1 Answers1

1

I'm not real familiar with numeraljs, but I think you may have to create a custom format.

Demo - codepen

Custom Format

numeral.register('format', 'thousands', {
regexps: {
    format: /(k)/,
    unformat: /(k)/
},
format: function(value, format, roundingFunction) {
    var space = numeral._.includes(format, ' k') ? ' ' : '',
        output;

    value = value*.001;

    format = format.replace(/\s?\k/, '');

    output = numeral._.numberToFormat(value, format, roundingFunction);

    if (numeral._.includes(output, ')')) {
        output = output.split('');

        output.splice(-1, 0, space + 'k');

        output = output.join('');
    } else {
        output = output + space + 'k';
    }

    return output;
}
});

Use Format

var number = 2000000; 
console.log(numeral(number).format('0k'));
Joe B.
  • 1,124
  • 7
  • 14