0

I've been trying to format number to currency using JavaScript, but every regex expression that I try doesn't work.

I have a paragraph:

<p class="text-primary" id="totalPaymentAllocatedLabel">Allocated Amount: $1,050.52</p>

Then I enter in a "number" input the value: 1150

Using JQuery, I try to edit the paragraph:

$("#totalPaymentAllocatedLabel").val(newValue.toFixed(2).replace(/(\d)(?=(\d{3})+$)/g, ""));

Then, it displays Allocated Amount: $1150.00 I would like $1,150.00

What am I doing wrong?

Thanks

Max Boy
  • 317
  • 6
  • 21
  • Sorry, I'm new and dumb with regex. Should I replace with value? – Max Boy Mar 15 '18 at 22:44
  • Possible duplicate of [How can I format numbers as dollars currency string in JavaScript?](https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-dollars-currency-string-in-javascript) – Taplar Mar 15 '18 at 22:46
  • see if this helps https://stackoverflow.com/questions/49261076/applying-currency-format-using-replace-and-a-regular-expression/49266204#49266204 – Scaramouche Mar 15 '18 at 22:55
  • Nope. There is no answer in the topic tat you sent – Max Boy Mar 15 '18 at 22:58
  • I thought you were trying to do something like obtaining `$1,150.00` from `1150`. – Scaramouche Mar 15 '18 at 23:01
  • 1
    https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-dollars-currency-string-in-javascript – SaganRitual Mar 15 '18 at 23:10

0 Answers0