How To Compare Two Currency In Jquery With Comma
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? Beca
Solution 1:
Here are a few ways to do it. I put the examples you posted in an array to avoid having 4 more variables.
const sampleInputs = [ '1,000', '10,000', '100,000', '1,000,000' ]
// + is a shortcut to convert to a number// split at commasconst splitMethod = +sampleInputs[0].split(',').join('')
// match digitsconst regexOne = +(sampleInputs[1].match(/\d/g) || []).join('')
// replace commasconst regexTwo = +sampleInputs[2].replace(/,/g, '')
// filterconst fi = +sampleInputs[3]
.split('')
.filter(n => n !== ',')
.join('')
console.log('splitMethod', splitMethod)
console.log('regexOne', regexOne)
console.log('regexTwo', regexTwo)
console.log('filter', fi)
Solution 2:
you can refer below line of code
functioncomparecurrent(cur1, cur2) {
if (parseInt(cur1.replace(/,/g, '')) > parseInt(cur2.replace(/,/g, ''))) {
alert("currency 1");
}
elseif (parseInt(cur1.replace(/,/g, '')) < parseInt(cur2.replace(/,/g, '')))
{
alert("currency 2");
}
else {
alert('equal');
}
}
Solution 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('');
Post a Comment for "How To Compare Two Currency In Jquery With Comma"