import lodash from 'lodash';

export const addCommas = (num, unit = '') => {
  return `${(Math.round(num) || 0).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,')} ${unit}`;
};

export const calcPercentage = (value = 0, total = 0, decimal = 0) => {
  if (total === 0) {
    return 0;
  }

  return lodash.round((value / total) * 100, decimal);
};

export const calcDiscount = (price, promotion, decimal = 0) => {
  if (promotion === price) {
    return 0;
  }

  return lodash.round(((price - promotion) / price) * 100, decimal);
};
