/**
 * Rounds a number like Excel's ROUND(number, num_digits):
 * - rounds half away from zero (so ROUND(-0.5) === -1, ROUND(2.5) === 3)
 * - supports negative `digits` (e.g. -1 rounds to the nearest ten)
 *
 * @param value number to round
 * @param digits number of decimal places (may be negative)
 * @returns the rounded number, or the original value if it is not finite
 */
export function excelRound(value: number, digits: number): number {
  if (!isFinite(value) || !isFinite(digits)) return value

  const factor = Math.pow(10, digits)

  return (Math.sign(value) * Math.round(Math.abs(value) * factor)) / factor
}
