const transFormatFrequency = (
  hertz: number,
  digits: number = 0
): string => {
  const yota = Math.pow(10, 24);
  if (hertz >= yota) {
    return `${(hertz / yota).toFixed(digits)} YHz`;
  }
  const format = ["Hz", "kHz", "MHz", "GHz", "THz", "PHz", "EHz", "ZHz"];

  const formatFrequency = (
    value: number,
    decimals: number,
    [currentLabel, ...otherLabels]: string[]
  ): string => {
    if (value < 1000) {
      return `${Number.parseFloat(value.toString()).toFixed(
        digits
      )} ${currentLabel}`;
    }
    return formatFrequency(value / 1000, digits, otherLabels);
  };

  return formatFrequency(hertz, digits, format);
};

주파수를 단위에 맞게 변환해주는 코드입니다.

 

 

사용하기

transFormatFrequency(1665000000, 2)

>>> 1.67 GHz 

``

+ Recent posts