JSFiddle - React, Tailwind, and code Playground

by A B Jones

JavaScript

function convertString(currency, input) {
  var thisInt = Math.round(input); //this removes numbers after decimal   
  var thisOutputValue = currency + commafy(thisInt); //add currency symbol and commas

  return thisOutputValue;
}


function commafy(num) {
  var str = num.toString().split('.');
  if (str[0].length >= 5) {
    str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,');
  }
  if (str[1] && str[1].length >= 5) {
    str[1] = str[1].replace(/(\d{3})/g, '$1 ');
  }
  return str.join('.');
}

var changeIntToCurrencyString = convertString('$', 435345.00) //change numbers into dollars

console.log(changeIntToCurrencyString)