how to compare two currency in jquery with comma

3481 views javascript
1

I have two inputs that when I start typing number, it automatically changes to currency, like this:

1,000
10,000
100,000
1,000,000

so how do you compare these two inputs?
Because it is a comma, it creates a comparative problem.

function priceCompare() {
    var price_meterVal;
    var priceVal;
    $("#price_meter").on("keyup",function () {
        price_meterVal = $($("#price_meter")).val().replace(/,/g, '');
    });
    $("#price").on("keyup",function () {
        priceVal = $($("#price")).val().replace(/,/g, '');
    });

    if (priceVal <= price_meterVal){
        $("#priceError").html('???? ?? ??? ??? ????? ???? ?? ???? ???? ????.');
        contractStatus = false;
    }else {
        contractStatus = true;
    }
}

answered question

Replace all the commas with empty strings, then compare

please share the html

Why are you wrapping a jQuery object in another jQuery object? $($("#price_meter)) is the same as $("#price_meter")... Also, the code you've shared replaces commas with empty strings and should therefore work for comparison. Please provide a Minimal, Complete, and Verifiable example which demonstrates the problem.

2 Answers

8

you can refer below line of code

function comparecurrent(cur1, cur2) {
        if (parseInt(cur1.replace(/,/g, '')) > parseInt(cur2.replace(/,/g, ''))) {
            alert("currency 1");
        }
        else if (parseInt(cur1.replace(/,/g, '')) < parseInt(cur2.replace(/,/g, ''))) 
        {
            alert("currency 2");
        }
        else {
            alert('equal');
        }
    }

posted this
3

let newInteger = parseInt(numberString.split(",").join(''));

I'm assuming you want it to be a number at the end to compare to other numbers. If you'd like to keep it a string let newString = numberString.split(",").join('');

posted this

Have an answer?

JD

Please login first before posting an answer.