📜  javascript intl.numberformat percent - Javascript (1)

📅  最后修改于: 2023-12-03 15:31:37.629000             🧑  作者: Mango

Javascript Intl.NumberFormat Percent

The Intl.NumberFormat object is used to format numbers in a locale-specific way. The percent style can be used to format numbers as percentages.

Usage

To format a number as a percentage using Intl.NumberFormat, you need to create a new Intl.NumberFormat object and specify the percent style. You can also specify the locale to use if you want to format the number according to a specific locale.

const number = 0.75;
const formatter = new Intl.NumberFormat('en-US', { style: 'percent' });
console.log(formatter.format(number)); // "75%"

In the example above, we create a new Intl.NumberFormat object for the en-US locale with the percent style. We then use the format method to format the number as a percentage.

Options

The Intl.NumberFormat constructor takes an options object that allows you to customize the formatting of the number.

locale

The locale option specifies the locale to use for formatting the number. The locale should be a string representing a valid BCP 47 language tag.

const number = 0.75;
const formatter = new Intl.NumberFormat('de-DE', { style: 'percent' });
console.log(formatter.format(number)); // "75 %"

In the example above, we create a new Intl.NumberFormat object for the de-DE locale with the percent style. We then use the format method to format the number as a percentage.

minimumFractionDigits

The minimumFractionDigits option specifies the minimum number of digits to use after the decimal point. The default value is 0.

const number = 0.75;
const formatter = new Intl.NumberFormat('en-US', { style: 'percent', minimumFractionDigits: 2 });
console.log(formatter.format(number)); // "75.00%"

In the example above, we create a new Intl.NumberFormat object for the en-US locale with the percent style and a minimumFractionDigits of 2. We then use the format method to format the number as a percentage.

maximumFractionDigits

The maximumFractionDigits option specifies the maximum number of digits to use after the decimal point. The default value is 0.

const number = 0.754321;
const formatter = new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 2 });
console.log(formatter.format(number)); // "75.43%"

In the example above, we create a new Intl.NumberFormat object for the en-US locale with the percent style and a maximumFractionDigits of 2. We then use the format method to format the number as a percentage.

minimumIntegerDigits

The minimumIntegerDigits option specifies the minimum number of digits to use before the decimal point. The default value is 1.

const number = 0.75;
const formatter = new Intl.NumberFormat('en-US', { style: 'percent', minimumIntegerDigits: 3 });
console.log(formatter.format(number)); // "075%"

In the example above, we create a new Intl.NumberFormat object for the en-US locale with the percent style and a minimumIntegerDigits of 3. We then use the format method to format the number as a percentage.

Conclusion

Intl.NumberFormat provides a way to format numbers as percentages in a locale-specific way. The percent style can be used to format numbers as percentages, and the options object allows for customization of the formatting.