Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting decimals from text element

I have an HTML element ("summary-payment") which contains the text: "Pay £100.00", for example.

I have managed to get the number 100 from this, as follows:

function getPaymentAmount(paymentAmount) {
    var stringTotal = $("#paymentBtn").text();
    var extractIntFromTotal = stringTotal.match(/\d+/)[0];
    return extractIntFromTotal;
}

However, I'd like to get the decimal 100.00 back. I have tried adding onto the index but this doesn't seem to work. Can anyone please help? I am very new to javascript.

Thank you :)

like image 998
Jordan1993 Avatar asked Jun 29 '26 00:06

Jordan1993


1 Answers

One way to achieve this would be to amend your regular expression to include the decimals:

function getPaymentAmount(paymentAmount) {
  return paymentAmount.match(/\d+[.,]?\d{2}?/)[0];
}

console.log(getPaymentAmount('Pay £100.00'));
console.log(getPaymentAmount('Pay £215'));
console.log(getPaymentAmount('Lorem ipsum $500.33 dolor sit'));
like image 78
Rory McCrossan Avatar answered Jun 30 '26 14:06

Rory McCrossan