繁体   English   中英

在javascript中将unicode转换为货币符号

[英]Converting unicode to currency symbol in javascript

我正在使用appcelerator中的货币符号来在Android和iOS中构建应用程序。 我想使许多参数动态化,因此将此值(u20b9)作为api传递给应用。 由于某些原因,无法像这样传递值(\\ u20b9),因此传递时请勿使用斜杠。

当我使用以下代码时,它可以正常工作:-

var unicode = '\u20b9';
alert(unicode);

输出:-₹

当我使用以下代码时:-

var unicode = '\\'+'u20b9';
alert(unicode);

输出:-\\ u20b9

因此,它在所有地方都打印\\ u20b9而不是₹,我不希望这样。

提前致谢。

以下对我有用:

console.log(String.fromCharCode(0x20aa)); // ₪ - Israeli Shekel
console.log(String.fromCharCode(0x24)); // $ - US Dollar
console.log(String.fromCharCode(0x20b9)); // ₹ - ???

alert(String.fromCharCode(0x20aa) + "\n" + String.fromCharCode(0x24) + "\n" + String.fromCharCode(0x20b9));

据我了解,您需要通过api传递Unicode字符的字符串值。 显然,不能在没有斜杠的情况下使用字符串代码,因为这会使它成为无效的unicode,如果传递斜杠,它将把值转换为unicode。 因此,您可以在此处传递不带斜杠和“ u”字符的字符串,然后将其余字符解析为十六进制格式。

请参见以下代码段:

// this won't work as you have included 'u' which is not a hexadecimal character
var unicode = 'u20b9';
String.fromCharCode(parseInt(unicode, 16));


// It WORKS! as the string now has only hexadecimal characters
var unicode = '20b9';
String.fromCharCode( parseInt(unicode, 16) ); // prints rupee character by using 16 as parsing format which is hexadecimal

希望它能解决您的查询!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM