test-publish-package-news
v2.0.0
Published
My first test publish package
Readme
My test-publish-package-news
Функция formatCurrency определена для форматирования числа в строку валюты и поддерживает несколько форматов валют. В коде также используется модульный подход к экспорту функций для легкого повторного использования в других местах.
A function formatCurrency is defined to format numbers into currency strings and supports multiple currency formats. The code also uses a modular approach to export functions for easy reuse in other places.
Installation
x Install the package using npm:
npm install test-publish-package-newsUsage
JavaScript
const formatCurrency = (amount, currency = "USD", decimalPlaces = 2) => {
const currencyOptions = {
USD: { style: "currency", currency: "USD" },
EUR: { style: "currency", currency: "EUR" },
CNY: { style: "currency", currency: "CNY" },
JPY: { style: "currency", currency: "JPY" },
GBP: { style: "currency", currency: "GBP" }
};
const options = currencyOptions[currency] || currencyOptions.USD;
if (currency === "JPY") {
decimalPlaces = 0;
}
return new Intl.NumberFormat("en-US", {
...options,
minimumFractionDigits: decimalPlaces,
maximumFractionDigits: decimalPlaces
}).format(amount);
};
module.exports = { formatCurrency };Use Node.js
// Импортирование модуля
const { formatCurrency } = require("./currencyFormatter");
// Форматирование суммы
console.log(formatCurrency(12345.678)); // По умолчанию USD, вывод: $12,345.68
console.log(formatCurrency(12345.678, "EUR")); // Вывод: €12,345.68
console.log(formatCurrency(12345.678, "CNY")); // Вывод: ¥12,345.68
console.log(formatCurrency(12345.678, "JPY")); // Вывод: ¥12,346 (иена без дробной части)
console.log(formatCurrency(12345.678, "GBP", 3)); // Вывод: £12,345.678