formatToCurrency function formats a numeric value into a currency string, allowing you to specify the currency and choose whether or not to display the currency symbol prefix.ts
import { formatToCurrency } from "@arkyn/shared";
valuenumbercurrencystringconfig (optional)objectshowPrefix (optional): boolean - Determines whether the currency symbol/prefix should be included. The default is true.string with the formatted currency. If config.showPrefix is false, the currency symbol is removed.Error("Unsupported currency code"): If the provided currency code is not supported.javascript
import { formatToCurrency } from "./formatToCurrency";// With prefixconst formattedUSD = formatToCurrency(1234.56, "USD");console.log(formattedUSD); // Output: "$1,234.56"// Without prefixconst withoutPrefixUSD = formatToCurrency(1234.56, "USD", {showPrefix: false,});console.log(withoutPrefixUSD); // Output: "1,234.56"
javascript
import { formatToCurrency } from "./formatToCurrency";// With prefixconst formattedBRL = formatToCurrency(1234.56, "BRL");console.log(formattedBRL); // Output: "R$ 1,234.56"// Without prefixconst withoutPrefixBRL = formatToCurrency(1234.56, "BRL", {showPrefix: false,});console.log(withoutPrefixBRL); // Output: "1,234.56"