0

In my system the var PRICE will have different formats depending on the CURRENCY configuration set by the user. So I can get values like:

a) price: $ 4.5 || b) price: 4.5 € || c) price: Bs. 4.5 || d) price: 4.5 ₵

I don't know whether the Currency symbol will be before or after the number and I don't know what symbol it is going to be (It's OpenERP framework so changing the format is not an option)

How do I get the number despite the currency symbol ??

4
  • Do you only want the number, or a value with a currency attribute? Commented Apr 2, 2014 at 15:07
  • Just look for numbers? /[\d.]+/? What do you care what symbols there are? Commented Apr 2, 2014 at 15:07
  • Maybe a "regex" is what I need, but I have no idea how to make it :( Commented Apr 2, 2014 at 15:08
  • I need the 4.5 as a number to use it in math operations Commented Apr 2, 2014 at 15:08

3 Answers 3

1

Once you get you string, your can replace every non digit character and the dot :

var number = '$ 4.5'.replace(/[^\d\.]/g, '');

Then you can parse it :

number = parseFloat(number);
//Alternatively
number = +number;
Sign up to request clarification or add additional context in comments.

Comments

1

I aproached your problem by using JQuery to replace any character that isnt a number or a '.' (dot).

I created a few spans with values in like this:

<span>$4.10</span>
  <span>£7.76</span>
  <span>€23.44</span>

then created some JQuery to alert the values:

$("span").each(function(){
  var a = $(this).text().replace(/[^0-9.]/g, "");
  alert(a);  
});

Here is a JSFiddle you can mess with: http://jsbin.com/xelameki/1/edit/

I hope it helps =)

1 Comment

It does work but the solution provided by Karl-André Gagnon is shorter . Thanxs for attending my question anyway :D
0

I made the following test:

$(document).ready(function(){
 var price1 = "$ 4.5"; 
 var price2 = "4.5 €"; 
 var price3 = "Bs. 4.5"; 
 var price4 = "4.5 ₵";
 $("#test").append(price1.split(" ")[1]);
 $("#test").append(price2.split(" ")[0]);
 $("#test").append(price3.split(" ")[1]);
 $("#test").append(price4.split(" ")[0]);
});

Working fiddle: http://jsfiddle.net/robertrozas/H4QUu/1/

1 Comment

As I said: "I don't know whether the Currency symbol will be before or after the number". Solution provided by Karl-André Gagnon is exactly what I need. Thanxs for attending my question anyway :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.