Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toLocaleString price without decimal zeroes

I am wondering how to efficiently remove the decimal zeroes from a price while keeping the 2 decimals if there

So if a price is 135.00 it should become 135.

If a price is 135.30 however it should keep both decimals.

If a price is 135.38 it can keep the decimals.

This is what I have at the moment:

const currency = 'EUR';
const language = 'NL';

var localePrice = (amount) => {
  const options = {
    style: 'currency',
    currency: currency
  };

  return amount.toLocaleString(language, options);
}

Now I could use regex or something similar but I am hoping there is an easier way to get this done.

I have made a JSFiddle which illustrates my problem which makes it easy to play with the code.

https://jsfiddle.net/u27a0r2h/2/

like image 840
Stephan-v Avatar asked Oct 25 '25 13:10

Stephan-v


1 Answers

Try just add minimumFractionDigits: 0 to your options, like:

const currency = 'EUR';
const language = 'NL';

var localePrice = (amount) => {
  const options = {
    style: 'currency',
    currency: currency,
    minimumFractionDigits: 0
  };

  return amount.toLocaleString(language, options);
}
like image 71
btavares Avatar answered Oct 28 '25 03:10

btavares