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.
\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
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 !