Search code examples
javascriptnansubtraction

JS subtracting numbers when one has decimal and does not


I have two vars that I need to subtract from one another.

My code is

var discountedTotal = totalAmount - discountAmount;

totalAmount is static and that value is 3,916.64

discountAmount is entered via an input and is a round number i.e. 500

so discountedTotal = 3,916.64 - 500;

I am getting a result of NAN, what is the best way to handle this scenario? Thanks


Solution

  • var totalAmount = '3,916.64';
    var discountAmount = 500;
    
    totalAmount = parseFloat(totalAmount.replace(',','')); 
    console.log(totalAmount); 
    discountedTotal = totalAmount - discountAmount;
    console.log(discountedTotal); 
    

    Try this. Hope it helps.