簡體   English   中英

如何在 JavaScript/Angular 中按貨幣名稱將數字格式化為貨幣

[英]how to format number to currency by currency name in JavaScript/Angular

如何按貨幣名稱將數字格式化為貨幣?

我有只有貨幣名稱的動態數字(例如:美元、印度盧比等)。 我沒有任何本地代碼(例如 en-US)。

我會嘗試以下方式。 但它要求提供本地代碼('en-US')。

var formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
});

formatter.format(2500)

但我的數據庫中沒有“en-US” 我只有貨幣名稱和數字。

那么如何通過貨幣名稱將數字格式化為貨幣?

只需為語言環境傳遞undefined

 const getFormattedCurrency = (currency, amount) => new Intl.NumberFormat(undefined, { style: 'currency', currency, }).format(amount); console.log(getFormattedCurrency('USD', 2500)); console.log(getFormattedCurrency('JPY', 2500)); console.log(getFormattedCurrency('EUR', 2500)); console.log(getFormattedCurrency('CNY', 2500)); console.log(getFormattedCurrency('AUD', 2500)); console.log(getFormattedCurrency('INR', 2500));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM