0

I'm a novice. I did research and was able to write this code that works great. The only issue is that when the user types the amount there are no commas separating the thousands, millions, etc. Does anyone have any ideas on modifying or adding something to code so that when the user types numbers the commas are added automatically? See code and screenshot below.

$w.onReady(function () {
    $w("#input247").onChange((Event) => {

let price = Number($w('#input247').value);
let traditionalPercent = Number ($w('#text556').text);
let buyersAgentPercent = Number ($w('#text557').text);
 

$w("#input247").value = null;

$w('#text548').text = '$' + price.toLocaleString();
$w('#text549').text = '$' + (price * traditionalPercent).toLocaleString();
$w('#text550').text = '$' + (price * buyersAgentPercent).toLocaleString();
$w('#text551').text = '$' + (price * traditionalPercent - price * buyersAgentPercent).toLocaleString();
    
    })})

screenshot

Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
  • See https://stackoverflow.com/questions/149055/how-to-format-numbers-as-currency-strings/16233919#16233919 – skara9 Dec 31 '21 at 04:36

2 Answers2

1

You can use javascript Intl.NumberFormat Internationalization API

let sale1 = 2500;
let sale2 = 762500;
let sale3 = 20660055500;
console.log(new Intl.NumberFormat().format(sale1));
console.log(new Intl.NumberFormat().format(sale2));
console.log(new Intl.NumberFormat().format(sale3));

for more info checkout https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

Bikale G
  • 67
  • 4
0

You can use regular expression. Try this

let numberFormat = (number) => parseFloat(number).toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
console.log(numberFormat(1000000));
ruleboy21
  • 5,510
  • 4
  • 17
  • 34