I have a prices:
var str1 = '10,00 €';
var str2 = '12.22 $';
I need get only currency symbol. I writed function:
function stringToCurrency(str){
return Number(str.replace("€", "").replace("$", "");
}
But this only replace currency symbol on ''
. How I can get currency symbol?
I have a prices:
var str1 = '10,00 €';
var str2 = '12.22 $';
I need get only currency symbol. I writed function:
function stringToCurrency(str){
return Number(str.replace("€", "").replace("$", "");
}
But this only replace currency symbol on ''
. How I can get currency symbol?
- Your function wouldn't work. You have a syntax error as there's a bracket missing. – Adrian Commented Apr 18, 2019 at 13:42
- @deceze He is not explicitly asking how to get the last character. You are making the assumption that the currency symbol will always be the last character, which is certainly not the case in all localizations. – devios1 Commented Apr 18, 2019 at 13:46
- I need get only currency symbol......why you are converting that to number? – Mamun Commented Apr 18, 2019 at 13:49
- 3 @devios1 From the sample data given, yes, I am assuming that. So does every answer so far. If that is not the case, the question needs to be clarified. – deceze ♦ Commented Apr 18, 2019 at 13:50
- 2 possible duplicate of how to get the last character of a string? – deceze ♦ Commented Apr 18, 2019 at 13:51
4 Answers
Reset to default 8If we use a regex to remove everything else (numbers, periods, mas, spaces) then we are only left with the currency symbols
var str1 = '10,00 €';
var str2 = '12.22 $';
function getCurrencySymbol(str) {
//replace all numbers, spaces, mas, and periods with an empty string
//we should only be left with the currency symbols
return str.replace(/[\d\., ]/g, '');
}
console.log(getCurrencySymbol(str1));
console.log(getCurrencySymbol(str2));
Just pick the last char of the string
function stringToCurrency(str) {
return str.trim().charAt(str.length - 1);
}
You can simply write a function that returns the last character of the string which represents the symbol that you want. Since Javascript string is a char array, you can access the last character by the length of the string as follows.
function stringToCurrency(str){
return str[str.length-1];
}
Hope It will help you! Thanks
The way Number()
works, is it returns an integer given any set of data. When you're passing these variables into it, like '10,00 €'
, the Number
function will return NaN because you're passing symbols and spaces into it.
To return only the symbol, JS has built-in methods that can be chained on to easily deal with this.
If the location is known, but value not, we can return the value of the given string location with charAt()
function knownLocation(s) {
return s.charAt(s.length -1)
}
If the location of that symbol is unknown, but you know it's there, we can check which is there with .includes
and then return the character at the index.
function knownValue(s) {
if (s.includes("$")) {
return s.charAt(s.indexOf("$"))
} else if (s.includes("€")) {
return s.charAt(s.indexOf("€"))
} else {
return undefined
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745501545a4630439.html
评论列表(0条)