0

I am writing a program for my class and we have not really gotten into how to format numbers with a comma, I have done some research and tried to use the toLocalestring(), but was not able to get it to work. I am not sure if I didn't put it in the right spot or if there is another way to put the comma where I want it. I will post my code to see if anyone has an idea of what I need to do. I only need the commas to show up in the balance and interest paid columns. Also I do have to use console.log() as per class requirement at this point.

function displayWelcome() {
  console.log("Welcome!");
  console.log("This program will determine the time to pay off a credit card and the interest paid based on the current balance, the interest rate, and the monthly payments made.");
}

function calculateminimumPaymentment(balance, minimumPaymentRate) {
  return Math.max(20, balance * minimumPaymentRate);
}

function displayPayments(balance, interest, minimumPayment) {

  var pad = 17;
  console.log("Balance on your credit card: $" + balance.toFixed(2));
  console.log("Interest Rate: " + (interest * 100) + "%");
  console.log("Assuming a minimum payment of 2% of the balance ($20 min)");
  console.log("Your minimum payment would be: $" + minimumPayment);
  console.log();
  console.log('Year'.padStart(pad), 'Balance'.padStart(pad - 10), 'Payment Number'.padStart(pad - 1), 'Interest Paid'.padStart(pad - 4), 'Minimum Payment'.padStart(pad - 3));
  var acum = 0;
  var yearcount = 0;
  var paynum = 0;
  var interestPaid = 1;
  var year = 0;


  while (balance > 0) {
    paynum++;
    interestPaid = balance * (interest / 12);
    balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
    minimumPayment = Math.max(20, balance * minimumPaymentRate);
    acum = (parseFloat(acum) + parseFloat(interestPaid)).toFixed(2);
    if (yearcount % 12 === 0) {
      year++;
      console.log(
        ('' + year).padStart(pad),
        '$' + parseFloat(balance).toFixed(2).padStart(pad - 9),
        ('' + paynum).padStart(pad - 3),
        '$' + parseFloat(interestPaid).toFixed(2).padStart(pad - 5),
        '$' + parseFloat(minimumPayment).toFixed(2).padStart(pad - 3)
      );
    } else {
      console.log(
        ''.padStart(pad),
        '$' + parseFloat(balance).toFixed(2).padStart(pad - 9),
        ('' + paynum).padStart(pad - 3),
        '$' + parseFloat(interestPaid).toFixed(2).padStart(pad - 5),
        '$' + parseFloat(minimumPayment).toFixed(2).padStart(pad - 3)
      );
    }
    yearcount++;
  }
}

var balance = 1500;
var minimumPaymentRate = 0.02;
var interest = 0.18;

displayWelcome();
var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate);

displayPayments(balance, interest, minimumPayment);
B.Moe09
  • 51
  • 2
  • 10
  • Try searching for similar problems before asking. There are many questions and answers for your problem. See https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-dollars-currency-string-in-javascript – Elias Soares Apr 15 '18 at 18:39
  • 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) – Elias Soares Apr 15 '18 at 18:39
  • May this ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat) helps you – Ravi Sevta Apr 15 '18 at 19:25

1 Answers1

0

You will have to format the number yourself. First convert it to string, then use regular expression to substitute the dot for a comma:

var a = 1234.5678;
var b = a.toFixed(2);
var c = b.replace(/\./, ',');

console.log(a, b, c);
Martin Adámek
  • 16,771
  • 5
  • 45
  • 64