
Originariamente inviata da
m4rko80
codice:
var total = "2.000,00"; //stringa presa dall'html
total = total.replace('.',''); //replace su stringa
alert(parseInt(total)); //ritorna 2000
occhio che se rimpiazzi la virgola dei decimali con nulla il 2000 poi diventa 200000
parseFloat()
Sei stato gentilissimo, grazie mille.
Riporto il codice completo funzionante, nel caso interessasse anche a qualcun altro.
codice HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento senza titolo</title>
<script type="text/javascript">
function formatMoney(number, places, symbol, thousand, decimal) {
number = number || 0;
places = !isNaN(places = Math.abs(places)) ? places : 2;
symbol = symbol !== undefined ? symbol : "$";
thousand = thousand || ",";
decimal = decimal || ".";
var negative = number < 0 ? "-" : "",
i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
}
var total = document.getElementById('TariffaNotti').innerHTML;
function test(item){
total = total.replace('.','');
total = parseInt(total);
if(item.checked){
total+= parseInt(item.value);
}else{
total-= parseInt(item.value);
}
//alert(total);
total = formatMoney(total,2, "", ".", ",");
document.getElementById('Totalcost').innerHTML = total;
}
</script>
</head>
<body>
<p>Tariffa per <b>1 notti</b>: <b><span id="TariffaNotti">2.000,00</span> euro</b>.</p>
<input type="checkbox" name="uno" value="30" onClick="test(this);" /> Extra n.1, euro 30,00<br />
<input type="checkbox" name="due" value="50" onClick="test(this);" /> Extra n.2, euro 50,00<br />
<input type="checkbox" name="tre" value="190" onClick="test(this);" /> Extra n.3, euro 190,00<br />
<b>Importo totale : <span id="Totalcost">2.000,00</span></b></p>
</body>
</html>