Convert Money to Number

[Total: 0    Average: 0/5]

This shows how to take a string that has dollar signs ($) and commas (,) in it and remove those characters and end up with a float or an integer value that cooresponds to that dollar string value.

Body Code:

<script type=text/javascript>
var _a = "$4,003,543.34";
document.write("Staring string: "+_a);
var _b = parseFloat(convertIt(_a));
var _c = parseInt(convertIt(_a));
document.write("<BR>Float = "+_b);
document.write("<BR>Integer = "+_c);
function convertIt(_a) {
  var _c = _a;
  var _x = _a.indexOf("$");
  if (_x == -1 ) _x = _a.indexOf(",");
  if (_x != -1) {
    var _p1 = _a.substr(0,_x);
	var _p2 = _a.substr(_x+1,_a.length);
    _c = convertIt(_p1+_p2);
  }
  return (_c);
}
</script>

Times Viewed: 29

Leave a Reply

Your email address will not be published. Required fields are marked *