0

I have a number variable that will always only provide digits like this: 249837

How can I format the string like this in javascript? 2,498.37

Edit: This is a bit more difficult from other questions because the last 2 digits in the input are cents with no periods in the variable.

obreezy
  • 303
  • 1
  • 5
  • 18
  • solved. Here's the function I created and does all the conditions I needed in my question. Hopefully it helps someone else: function formatPrice(number) { var price1 = number.toString(); var price_length = price1.length; var price_pos = price_length - 2; var price2 = price1.substr(0, price_pos) + "." + price1.substr(price_pos); var price3 = Number(price2).toFixed(2); var price4 = price3.replace(/\d(?=(\d{3})+\.)/g, '$&,'); var formattedPrice = price4.replace(/\.00$/,''); return formattedPrice } – obreezy Nov 04 '18 at 21:15
  • jfiddle with solution: https://jsfiddle.net/omarel/xpvt214o/922930/ – obreezy Nov 04 '18 at 21:22

0 Answers0