我想将这两个字符串类型的值相乘
值 1 = "$10.99"
值 2 = "$20.8"
var x = document.getElementById("Text1");
var y = document.getElementById("Text2");
var tointx = x.Value
var tointy = y.Value
var z = tointx * tointy
var result = z.reduc((r, e) => r + +e.replace(',', '.'), 0)
alert(result);
<input id="Text1" value="$10.99" />
<input id="Text2" value="$20.8" />
最佳答案
几件事
.value
JS区分大小写const x = document.getElementById("Text1").value;
const y = document.getElementById("Text2").value;
let tofloatx = +x.slice(1); // unary plus will convert to number but so will multiplication
let tofloaty = +y.slice(1)
let z = tofloatx * tofloaty
console.log("float",z,"int",Math.round(z))
<input id="Text1" value="$10.99" />
<input id="Text2" value="$20.8" />
https://stackoverflow.com/questions/69692015/