Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 206
Homer Avatar asked May 06 '15 21:05

Homer


People also ask

How do I limit decimal places in JavaScript?

To limit decimal places in JavaScript, use the toFixed() method by specifying the number of decimal places. This method: Rounds the number. Converts it into a string.

How do I get 2 decimal places in JavaScript?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result.

How do you check whether a number is decimal or not in JavaScript?

JavaScript Code:function number_test(n) { var result = (n - Math. floor(n)) !== 0; if (result) return 'Number has a decimal place. '; else return 'It is a whole number.

How do I show two decimal places in typescript?

Use the toFixed() method in JavaScript to format a number with two decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.


2 Answers

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 
like image 168
idrosid Avatar answered Sep 19 '22 21:09

idrosid


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).

like image 37
Stuck Avatar answered Sep 20 '22 21:09

Stuck