All files / src/functions roundNumber.ts

57.14% Statements 12/21
40% Branches 4/10
50% Functions 1/2
57.14% Lines 12/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 3633x   554x 67x 487x   28x   28x 28x   459x   459x   459x   459x 459x                                  
export const round_to_sf_dp = (value: number) => {
  // round to 0 dp or 4 sf, whichever has more numbers
  if (value === 0) {
    return 0;
  } else if (value >= 1000) {
    // 2 dp
    const dp_value = value.toFixed(2);
    // remove trailing zeros
    const db_value_trimmed = dp_value.replace(/\.?0+$/, "");
    return db_value_trimmed;
  } else {
    const tol = 8;
    // round to tol dp
    const tol_value = parseFloat(value.toFixed(tol));
    // round to 5 sf
    const sf_value = parseFloat(tol_value.toPrecision(5)).toFixed(10);
    // remove trailing zeros
    const sf_value_trimmed = sf_value.replace(/\.?0+$/, "");
    return sf_value_trimmed;
  }
};
 
export function valueFormatter(value: number) {
  let returnValue: number | string = value;
  const numberArray = value.toString().split(".");
  const integerPart = numberArray[0];
  const decimalPart = numberArray[1] ? numberArray[1].padEnd(4, "0") : "";
  Iif (decimalPart != "" && decimalPart.length > 4) {
    returnValue = +value.toFixed(4);
  }
  Iif (integerPart.length > 6) {
    returnValue = value.toExponential(4);
  }
  return returnValue;
}