Ahmad Ali
Ahmad Ali

Reputation: 43

How to multiply two decimals of type string javascript

I want to multiply these two values ​​of type string

values 1 = "$10.99"

values 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" />

Upvotes: 2

Views: 100

Answers (3)

Bekim Bacaj
Bekim Bacaj

Reputation: 5955

multiplyFields();

function multiplyFields( ) {
 var x = Text1.value,
     y = Text2.value,
     z = x.match( /[0-9.]+/ ) * y.match( /[0-9.]+/ );

    alert( z );
}
<input id=Text1 value=$10.99 >
<input id=Text2 value=$20.80 >

Upvotes: 2

Jip Helsen
Jip Helsen

Reputation: 1346

Is this what you needed?

let x = "$10.99"
let y = "$20.8"
let moneyToNb = function(money) {return Number(money.replace("$",""))}
let intX = moneyToNb(x)
let intY = moneyToNb(y)
let z = Math.floor(intX*intY)
console.log(z);

Upvotes: 2

mplungjan
mplungjan

Reputation: 177691

Several things

  1. .value JS is case sensitive
  2. $ sign needs to go
  3. reduce is misspelled too but not needed
  4. Are you sure you want ints?

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" />

Upvotes: 3

Related Questions