最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Converting unicode to currency symbol in javascript - Stack Overflow

programmeradmin6浏览0评论

I am working with currency symbol in appcelerator for building apps in Android and iOS. I want make many parameters dynamic, so passing this value(u20b9) as api to app. Can't pass value(\u20b9) like this because of some reasons, so passing without slash.

When I use below code it works proper:-

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

Output:- ₹

When I use below code:-

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

Output:- \u20b9

Because of this, instead of ₹ it prints \u20b9 everywhere, which I don't want.

Thanks in advance.

I am working with currency symbol in appcelerator for building apps in Android and iOS. I want make many parameters dynamic, so passing this value(u20b9) as api to app. Can't pass value(\u20b9) like this because of some reasons, so passing without slash.

When I use below code it works proper:-

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

Output:- ₹

When I use below code:-

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

Output:- \u20b9

Because of this, instead of ₹ it prints \u20b9 everywhere, which I don't want.

Thanks in advance.

Share asked May 29, 2018 at 12:44 Vishal RabadiyaVishal Rabadiya 5155 silver badges24 bronze badges 4
  • The \u escapes are parsed when the JavaScript parser encounters the string constant. Thus "\u20b9" does exactly the same thing as "₹" does. It's really not clear what you're asking, though perhaps String.fromCharCode() is what you're looking for. – Pointy Commented May 29, 2018 at 12:47
  • 2 "Can't pass value(\u20b9) like this because of some reasons" Then fix those reasons. Don't work around the real problem. Don't hack together a solution. – Cerbrus Commented May 29, 2018 at 12:48
  • can't you just pass the unicode character as it is through the API, so just the ₹ and display it? – miga Commented May 29, 2018 at 13:13
  • 2 now passing currency symbol, instead of unicode. Passing unicode reason was, if currency symbol don't support in all device. @miga currency does symbol supports in all Android and iOS devices? – Vishal Rabadiya Commented May 29, 2018 at 13:20
Add a ment  | 

2 Answers 2

Reset to default 6

The following works for me:

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));

As far I understand, you need to pass string values of unicode characters via api. Obviously you can't use string-code without slash because that will make it invalid unicode and if you pass the slash that'll convert the value to unicode. So what you can do here is to pass the string without slash & 'u' character and then parse the remaining characters as hexadecimal format.

See following code snippet:

// 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

I hope it solves your query !

发布评论

评论列表(0)

  1. 暂无评论