export default class NumberUtil {
  public static decimalPart(number_: number): number {
    return Number((number_ % 1).toFixed(2));
  }

  public static getValidNumber(number?: number | string | null): number | null {
    if (number === null) {
      return null;
    }
    if (typeof number === "string" && number === "") {
      return null;
    }
    const value = Number(number);
    if (Number.isNaN(value)) {
      return null;
    }
    return value;
  }

  public static getValidNumberOrThrow(number?: number | string | null): number {
    const valid = this.getValidNumber(number);
    if (typeof valid !== "number" || Number.isNaN(valid)) {
      throw new Error(`Invalid number='${number}'`);
    }
    return valid;
  }
}
