Homer
Homer

Reputation: 7806

Is there a format for numeral.js that will show decimals only when needed?

tests: http://jsfiddle.net/su918rLv/

This is the how the existing formats work:

numeral(1234.567).format('0,0');
//output: 1,235

numeral(1234.567).format('0,0.00');
//output: 1,234.57    

numeral(1234).format('0,0.00');
//output: 1,234.00

Is there a format that will produce both a whole number or decimal number based on the number value? I'm using 0,0.99 here but it is not the answer.

numeral(1234).format('0,0.99');
//output: 1,234

numeral(1234.567).format('0,0.99');
//output: 1,234.57

Upvotes: 37

Views: 29892

Answers (2)

Stuck
Stuck

Reputation: 12292

Either no decimals or 2 decimals:

numeral(1234).format('0,0[.]00');
// output: 1,234

numeral(1234.5).format('0,0[.]00');
// output: 1,234.50

(Based on johnwp's comment from the accepted answer).

Upvotes: 7

idrosid
idrosid

Reputation: 8029

Yes. Put the optional decimals in brackets:

numeral(1234).format('0,0.[00]');
// output: 1,234

numeral(1234.567).format('0,0.[00]');
// output: 1,234.57

Upvotes: 58

Related Questions