toLocaleString - only show decimals if not .00

I am creating a calculator where I need to format some numbers, which I am doing using toLocaleString. My question is how can I make it so that it only shows the decimal points if they're necessary(IE if the number is 127.00 it would just show as 127, but if it's 127.50 then it will show 127.50)

$('div').each(function() {
  $(this).text($(this).attr('data-val').toLocaleString('en', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  }));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<div data-val="127.00"></div>
<div data-val="127.50"></div>

There's no option for something like that in toLocaleString so I personally would go for an obvious choice to me. ie: There may be better options.

This method splits the number at the decimal and then checks to see if the number after it is '00'. If it is, then it sets the text to the part before the decimal.

This does assume that there is a decimal and will fail if there isn't. But it's simple enough to check parts.length to find out if there is or not.

$('div').each(function() {
  var formatted = $(this).attr('data-val').toLocaleString('en', 
    { minimumFractionDigits: 2, maximumFractionDigits: 2 }
  )
  var parts = formatted.split('.')
  if (parts.length > 1 && parts[1] === '00') {
    formatted = parts[0]
  }
  $(this).text(formatted);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-val="127.00"></div>
<div data-val="127.50"></div>
<div data-val="127"></div>

Number.prototype.toLocaleString(), toLocaleString()); // Displays "3,500" if in U.S. English locale The locales and options arguments are not supported in all browsers yet. uses comma as decimal separator and period for thousands console.log(number. is the default language, or // → "30 000,65" where French is the default language  This JavaScript tutorial explains how to use the Number method called toLocaleString() with syntax and examples. In JavaScript, toLocaleString() is a Number method that is used to convert a number into a locale-specific numeric representation of the number (rounding the result where necessary) and return its value as a string.

You can use the following snippet:

$('div').each(function() {
  var formatted = $(this).attr('data-val').toLocaleString('en', 
    { minimumFractionDigits: 2, maximumFractionDigits: 2 }
  )
  formatted = (formatted % 1 > 0) ? formatted : Math.round(formatted);
  $(this).text(formatted);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-val="127.00"></div>
<div data-val="127.50"></div>
<div data-val="127"></div>

JavaScript: Outputting a Number in Traditional Currency Format , Also, you get the currency symbol, which is not always desirable. By default the toLocaleString function shows no decimals if none were provided However anything less and the JavaScript would only show that number of decimals, so if the user { currency: 'EUR', locale: 'fr' }); // Returns 2 534 234,00 €. MDN will be in maintenance mode for a brief period Tuesday June 2, from around 10:00 AM until no later than 12:00 PM Pacific (in UTC, Tuesday June 2, 5:00 PM to 7:00 PM).

If you just want to remove '.00', you could replace them.

$('div').each(function() {
  $(this).text($(this).attr('data-val').toLocaleString('en', {
    minimumFractionDigits: 2, maximumFractionDigits: 2})
   .replace('.00',''));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<div data-val="127.00"></div>
<div data-val="127.50"></div>
<div data-val="127"></div>

NumberFormat class - intl library - Dart API, In prefix or suffix, multiply by 100 and show as percentage; ‰ (\u2030) In prefix or suffix, multiply by 1000 and show as per mille; ¤ (\u00A4) Currency If the format is not specified it will print in a basic format with at least one integer digit prints only as equivalent to "#E0" and does not take into account significant digits. Decimal: 0. Currency: The default follows the selected currency rules. If none is supplied by the currency it will fall back to 2. Percent: 0. maximumFractionDigits. Possible values are from 0 to 20. Default values (If minimumFractionDigits is larger than the default value below then it will be used instead): Decimal: 3.

JavaScript: Number toLocaleString() method, In JavaScript, toLocaleString() is a Number method that is used to convert a This website would not exist without the advertisements we display and your kind If omitted, the default for decimal is 0, the default for percent is 0, and the default  By default the toLocaleString function shows no decimals if none were provided by the user… it will just show a whole number… or just a single decimal if one place was provided. This makes for some messy output. So what you need to do is to set two options: the maximumFractionDigits and the minimumFractionDigits.

JavaScript toFixed() Method, Note: if the desired number of decimals are higher than the actual number, zeros are Convert a number which has fewer decimal places than requested:. RE: Show decimal point only if not a whole number. Right-click > FormatCells > NumberTab > Custom > 0.## in the Type: window >. Okthis will format to give a decimal point and no digits after for. whole numbers and one to two decimal places max for those numbers with. decimals.

Formatting Numbers – amCharts 4 Documentation, Number formatting in amCharts is loosely based on Unicode standard. Important: if dot is missing, the formatter will not round or format decimals, and will display the number as is. Check "Modifying big number prefixes" for more information about prefixes. Example format: 'The value is:' #,###.00 '(in million US$)'. Definition and Usage. The toLocaleString() method converts a number into a string, using a local language format. The default language depends on the locale setup on your computer.