Coding Challenge - #13: Format Number Commas

Write a function formatNumber(n) that accepts a number or numeric string and returns a string with comma separators added to the integer portion. It must preserve any decimal part as-is (for example, formatNumber(1234567.89) returns '1,234,567.89').

function formatNumber(n) {
  // your code here
}

Rules:

  • Do not use Intl.NumberFormat or toLocaleString()
  • Must support negative numbers (e.g., -1234.5 becomes '-1,234.5')
  • Preserve decimal values exactly without rounding

Post your solution as a reply. Answer goes up in about a day.