如何在javascript中使用toLocaleString()和tofixed(2)

时间:2022-09-27 14:51:40

How can i do this in javascript ?

我怎么能在JavaScript中执行此操作?

var num = 2046430; 
num.toLocaleString();

will give you "2,046,430";

What i have tried is

我试过的是

var num = 2046430; 
num.toLocaleString().toFixed(2);

Expected Output

"2,046,430.00";

1 个解决方案

#1


86  

Taken from MDN:

摘自MDN:

Syntax

numObj.toLocaleString([locales [, options]])

numObj.toLocaleString([locales [,options]])

toLocaleString takes 2 arguments. The first is the locale, the second are the options. As for the options, you are looking for:

toLocaleString需要2个参数。第一个是区域设置,第二个是选项。至于选项,您正在寻找:

minimumFractionDigits

The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).

要使用的最小小数位数。可能的值为0到20;普通数字和百分比格式的默认值为0;货币格式的默认值是ISO 4217货币代码列表提供的次要单位数的数量(如果列表未提供该信息,则为2)。

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

To be able to set the options without setting the locale, you can pass undefined as first argument:

为了能够在不设置区域设置的情况下设置选项,您可以将undefined作为第一个参数传递:

var num = 2046430;
num.toLocaleString(undefined, {minimumFractionDigits: 2}) // 2,046,430.00

However this also allows the fraction to be longer than 2 digits. So we need to look for one more option called maximumFractionDigits. (Also on that MDN page)

然而,这也允许分数长于2位。因此,我们需要寻找另一个名为maximumFractionDigits的选项。 (也在MDN页面上)

var num = 2046430.123;
num.toLocaleString(undefined, {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
}) // 2,046,430.12

#1


86  

Taken from MDN:

摘自MDN:

Syntax

numObj.toLocaleString([locales [, options]])

numObj.toLocaleString([locales [,options]])

toLocaleString takes 2 arguments. The first is the locale, the second are the options. As for the options, you are looking for:

toLocaleString需要2个参数。第一个是区域设置,第二个是选项。至于选项,您正在寻找:

minimumFractionDigits

The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).

要使用的最小小数位数。可能的值为0到20;普通数字和百分比格式的默认值为0;货币格式的默认值是ISO 4217货币代码列表提供的次要单位数的数量(如果列表未提供该信息,则为2)。

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

To be able to set the options without setting the locale, you can pass undefined as first argument:

为了能够在不设置区域设置的情况下设置选项,您可以将undefined作为第一个参数传递:

var num = 2046430;
num.toLocaleString(undefined, {minimumFractionDigits: 2}) // 2,046,430.00

However this also allows the fraction to be longer than 2 digits. So we need to look for one more option called maximumFractionDigits. (Also on that MDN page)

然而,这也允许分数长于2位。因此,我们需要寻找另一个名为maximumFractionDigits的选项。 (也在MDN页面上)

var num = 2046430.123;
num.toLocaleString(undefined, {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
}) // 2,046,430.12