0

Possible Duplicate:
Remove characters from a string

I have a variable p which I would like to remove the $ from. This variable will be a number such as $10.56. How can I do this? I thought it could be done using .replace('$','') but i'm not quite sure how to implement this.

Here is my javascript code:

function myFunction() {
  var p = parseFloat(document.getElementById('p_input').value);
  var q = parseFloat(document.getElementById('q_input').value);
  if (!q){
  document.getElementById('t').value = '';
  }
  else {
  var t = q * p;
  document.getElementById('t_output').value = t;
  }
}
Community
  • 1
  • 1
rural.user
  • 75
  • 2
  • 6

2 Answers2

6

It's pretty simple:

var myString = "$15.62"
console.log(myString.replace('$', ''));
//Logs: "15.62"

Please note that this new value is not actually "saved" to myString, you'll have to assign it to a variable, yourself:

var newString = myString.replace('$', '');
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
2

Try this, assuming that the values of p_input and q_input will be the money values:

var p = parseFloat(document.getElementById('p_input').value.replace('$', ''));
var q = parseFloat(document.getElementById('q_input').value.replace('$', ''));
Stegrex
  • 4,004
  • 1
  • 17
  • 19