Apply String method on Integers

Tell us what’s happening:
I have a function which has a String replace method, I am passing some data from an Object as a parameter to the function which is used by that replace method, in which data is saved as Integers.

Your code so far


  let formatNumbers = function (num, type) {
    if (type === "inc") {
      return "+ " + num.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
    } else if (type === "exp") {
      return "- " + num.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
    }
  };

  let showTotal = function (obj) {
      DOMNames.totalBudget.textContent = obj.totalIncome > obj.totalExpense ? formatNumbers(obj.budget, "inc") : formatNumbers(obj.budget, "exp");
      DOMNames.totalIncome.textContent = formatNumbers(obj.totalIncome, "inc");
      DOMNames.totalExpPercentage.textContent = obj.percentage + " %";
      DOMNames.totalExpense.textContent = formatNumbers(obj.totalExpense, "exp");
    }
  
  /*
    obj.budget
    obj.totalIncome
    obj.totalExpense
    All these three values have been saved inside an object and their type is number

    I cannot save these items as Strings since these values are used by other functions for some calculation

    I tried to wrap the data as below but it is not working

    DOMNames.totalIncome.textContent = formatNumbers(toString(obj.totalIncome), "inc");
    Still it is giving me the error that num.replace ........... is not a function
  */

I’m assuming you’re looking for:

String() and not toString()

Ok, Thanks bro. Let me check

Many Thanks @kerafyrm02, It resolved the issue.