lib/index.js
function commaNumber(inputNumber, optionalSeparator, optionalDecimalChar) {

  // we'll strip off and hold the decimal value to reattach later.
  // we'll hold both the `number` value and `stringNumber` value.
  let number, stringNumber, decimal

  // default `separator` is a comma
  const separator = optionalSeparator   || ','

  // default `decimalChar` is a period
  const decimalChar = optionalDecimalChar || '.'

  switch (typeof inputNumber) {

    case 'string':

      // if there aren't enough digits to need separators then return it
      // NOTE: some numbers which are too small will get passed this
      //       when they have decimal values which make them too long here.
      //       but, the number value check after this switch will catch it.
      if (inputNumber.length < (inputNumber[0] === '-' ? 5 : 4)) {
        return inputNumber
      }

      // remember it as a string in `stringNumber` and convert to a Number
      stringNumber = inputNumber

      // if they're not using the Node standard decimal char then replace it
      // before converting.
      number = decimalChar !== '.' ? Number(stringNumber.replace(decimalChar, '.'))
                                   : Number(stringNumber)
      break

    // convert to a string.
    // NOTE: don't check if the number is too small before converting
    //       because we'll need to return `stringNumber` anyway.
    case 'number':
      stringNumber = String(inputNumber)
      number       = inputNumber
      break

    // return invalid type as-is
    default: return inputNumber
  }

  // when it doesn't need a separator or isn't a number then return it
  if ((-1000 < number && number < 1000) || isNaN(number) || !isFinite(number)) {
    return stringNumber
  }

  // strip off decimal value to append to the final result at the bottom
  let decimalIndex = stringNumber.lastIndexOf(decimalChar)

  if (decimalIndex > -1) {
    decimal = stringNumber.slice(decimalIndex)
    stringNumber = stringNumber.slice(0, -decimal.length)
  }

  // else {
  //   decimal = null
  // }

  // finally, parse the string and add in separators
  stringNumber = parse(stringNumber, separator)

  // if there's a decimal value add it back on the end.
  // NOTE: we sliced() it off including the decimalChar, so it's good.
  return decimal ? stringNumber + decimal : stringNumber

}
function commaNumber(inputNumber, optionalSeparator, optionalDecimalChar) {

  // we'll strip off and hold the decimal value to reattach later.
  // we'll hold both the `number` value and `stringNumber` value.
  let number, stringNumber, decimal

  // default `separator` is a comma
  const separator = optionalSeparator   || ','

  // default `decimalChar` is a period
  const decimalChar = optionalDecimalChar || '.'

  switch (typeof inputNumber) {

    case 'string':

      // if there aren't enough digits to need separators then return it
      // NOTE: some numbers which are too small will get passed this
      //       when they have decimal values which make them too long here.
      //       but, the number value check after this switch will catch it.
      if (inputNumber.length < (inputNumber[0] === '-' ? 5 : 4)) {
        return inputNumber
      }

      // remember it as a string in `stringNumber` and convert to a Number
      stringNumber = inputNumber

      // if they're not using the Node standard decimal char then replace it
      // before converting.
      number = decimalChar !== '.' ? Number(stringNumber.replace(decimalChar, '.'))
                                   : Number(stringNumber)
      break

    // convert to a string.
    // NOTE: don't check if the number is too small before converting
    //       because we'll need to return `stringNumber` anyway.
    case 'number':
      stringNumber = String(inputNumber)
      number       = inputNumber
      break

    // return invalid type as-is
    default: return inputNumber
  }

  // when it doesn't need a separator or isn't a number then return it
  if ((-1000 < number && number < 1000) || isNaN(number) || !isFinite(number)) {
    return stringNumber
  }

  // strip off decimal value to append to the final result at the bottom
  let decimalIndex = stringNumber.lastIndexOf(decimalChar)

  if (decimalIndex > -1) {
    decimal = stringNumber.slice(decimalIndex)
    stringNumber = stringNumber.slice(0, -decimal.length)
  }

  // else {
  //   decimal = null
  // }

  // finally, parse the string and add in separators
  stringNumber = parse(stringNumber, separator)

  // if there's a decimal value add it back on the end.
  // NOTE: we sliced() it off including the decimalChar, so it's good.
  return decimal ? stringNumber + decimal : stringNumber

}
function commaNumber ( inputNumber , optionalSeparator , optionalDecimalChar ) { // we'll strip off and hold the decimal value to reattach later. // we'll hold both the `number` value and `stringNumber` value. let number , stringNumber , decimal // default `separator` is a comma const separator = optionalSeparator || ',' // default `decimalChar` is a period const decimalChar = optionalDecimalChar || '.' switch ( typeof inputNumber ) { case 'string' : // if there aren't enough digits to need separators then return it // NOTE: some numbers which are too small will get passed this //       when they have decimal values which make them too long here. //       but, the number value check after this switch will catch it. if ( inputNumber . length < ( inputNumber [ 0 ] === '-' ? 5 : 4 ) ) { return inputNumber } // remember it as a string in `stringNumber` and convert to a Number stringNumber = inputNumber // if they're not using the Node standard decimal char then replace it // before converting. number = decimalChar !== '.' ? Number ( stringNumber . replace ( decimalChar , '.' ) ) : Number ( stringNumber ) break // convert to a string. // NOTE: don't check if the number is too small before converting //       because we'll need to return `stringNumber` anyway. case 'number' : stringNumber = String ( inputNumber ) number = inputNumber break // return invalid type as-is default : return inputNumber } // when it doesn't need a separator or isn't a number then return it if ( ( - 1000 < number && number < 1000 ) || isNaN ( number ) || ! isFinite ( number ) ) { return stringNumber } // strip off decimal value to append to the final result at the bottom let decimalIndex = stringNumber . lastIndexOf ( decimalChar ) if ( decimalIndex > - 1 ) { decimal = stringNumber . slice ( decimalIndex ) stringNumber = stringNumber . slice ( 0 , - decimal . length ) } // else { //   decimal = null // } // finally, parse the string and add in separators stringNumber = parse ( stringNumber , separator ) // if there's a decimal value add it back on the end. // NOTE: we sliced() it off including the decimalChar, so it's good. return decimal ? stringNumber + decimal : stringNumber }
return a string with the provided number formatted with commas. can specify either a Number or a String.
return a string with the provided number formatted with commas . can specify either a Number or a String .