2017-08-29 99 views
1

如何将货币HTML字符串转换为JavaScript中的符号。 这是来自API的响应。 例如£需要转换为£获取货币符号的特殊文本字符的HTML实体代码

你可以在这里查看我的API响应:

> getOptions: { 
>    "FKP": "Falkland Islands pound (£)", 
>    "GBP": "Pound sterling (£)", 
>    "GEL": "Georgian lari (ლ)", 
>    "GGP": "Guernsey pound (£)", 
>  } 


Please suggest. 
https://html-online.com/editor/ 

Following this link if you are click this link (just paste inside in this link `£` automatically converted pounds Symbol like £) 
+0

你到底要转换?一种货币到另一种货币或货币代码到货币符号。 –

+0

我仍然有56个货币代码,我想都是。我正在使用货币代码,它将显示符号 – Lavaraju

+0

检查此https://stackoverflow.com/questions/19373860/convert-currency-names-to-currency-symbol –

回答

1

是的,我终于得到了一个绝对的解决方案来看看here。 就像我刚刚安装了npm install html-entities这个。之后,当我们正在使用特定的地方一样, 两个两个是给这个

const Entities = require('html-entities').AllHtmlEntities; 

const entities = new Entities(); 

之后console.log(entities.decode('£'))得到结果输出像£

请参考以下链接https://www.npmjs.com/package/html-entities

+0

如果这是你的答案,你应该接受它,所以它可以作为一个重复引用,也有人知道有一个在搜索结果中接受的答案。 – RobG

0
var currency_symbol={ 
     "FKP": "¤", 
     "GBP": "£", 
     "GEL": "₡", 
     "GGP": "₲", 
}, 

Now get it by-- 
currency_symbol.FKP 
currency_symbol.GBP etc 
+0

但在我的服务响应在作为货币符号该字符串这样的波纹管, VAR CURRENCY_SYMBOL = { “英镑”: “£”, “GGP”: “£”, }, 如果我晋currency_symbol.GBP在显示£文本不显示货币符号 – Lavaraju

+0

确保你的文档在UTF-8, 和更多的信息,你可以尝试在W3C hool.com

我会显示£

否则,我认为你应该定制你的回应。 –

0

您problably使用字符串在某种情况下的,要么逃脱HTML控制字符(如&)或不被解释/显示作为HTML内容,所以£字符串将按字面显示。

一种解决方法是不使用HTML实体,如£但实际的Unicode字符的各种货币符号:

getOptions: { 
    "FKP": "Falkland Islands pound (\u00A3)", 
    "GBP": "Pound sterling (\u00A3)", 
    "GEL": "Georgian lari (\u10DA)", 
    "GGP": "Guernsey pound (\u00A3)", 
} 

供您参考,也看到这个图表的Unicode与other currency symbols

0

您可以使用Intl进行格式化,使用语言环境和货币代码来选择格式。

var number = 123456.789; 
 

 
// request a currency format 
 
console.log(new Intl.NumberFormat('de-DE', { 
 
    style: 'currency', 
 
    currency: 'EUR' 
 
}).format(number)); 
 
// → 123.456,79 € 
 

 
// the Japanese yen doesn't use a minor unit 
 
console.log(new Intl.NumberFormat('ja-JP', { 
 
    style: 'currency', 
 
    currency: 'JPY' 
 
}).format(number)); 
 
// → ¥123,457 
 

 
// limit to three significant digits 
 
console.log(new Intl.NumberFormat('en-IN', { 
 
    maximumSignificantDigits: 3 
 
}).format(number)); 
 
// → 1,23,000

采取更多信息

+0

您是否尝试过使用FKP? – RobG

+0

货币代码列表包括它(我没有测试),你可以在这里找到代码列表:https://www.currency-iso.org/dam/downloads/lists/list_one.xml –

+0

在所有的实现我测试过,使用FKP返回货币代码(即FKP),而不是符号(£),这不是OP想要的。 – RobG