admin管理员组

文章数量:1022726

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?

Share Improve this question asked Apr 18, 2019 at 13:39 DronaxDronax 2892 gold badges6 silver badges17 bronze badges 8
  • 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
 |  Show 3 more ments

4 Answers 4

Reset to default 8

If 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
 }
}

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?

Share Improve this question asked Apr 18, 2019 at 13:39 DronaxDronax 2892 gold badges6 silver badges17 bronze badges 8
  • 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
 |  Show 3 more ments

4 Answers 4

Reset to default 8

If 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
 }
}

本文标签: javascriptGet currency symbol from a string priceStack Overflow