f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/main/js/formulas/math/QUOTIENT.ts
-rw-r--r--
1304
 1import { DivException } from "../../errors/DivException";
 2import { F7Exception } from "../../errors/F7Exception";
 3import { SheetColumnRowKey } from "../../models/common/SheetColumnRowKey";
 4import { Complex } from "../../models/common/Types";
 5import { Converters } from "../../utils/Converters";
 6import { Numbers } from "../../utils/Numbers";
 7import { AbstractFormula } from "../AbstractFormula";
 8import { FormulaName } from "../FormulaName";
 9import { ROUNDDOWN } from "./ROUNDDOWN";
10
11export class QUOTIENT extends AbstractFormula {
12  static SELF: QUOTIENT = new QUOTIENT();
13  NAME = FormulaName.QUOTIENT;
14
15  internal(origin: SheetColumnRowKey, ...values: Array<Complex>) {
16    AbstractFormula.checkLength(values.length, 2, this.NAME);
17    const first = Converters.first(this.collateralLookup(origin, values[0]));
18    const second = Converters.first(this.collateralLookup(origin, values[1]));
19    if (first instanceof F7Exception) {
20      return first;
21    }
22    if (second instanceof F7Exception) {
23      return second;
24    }
25    const dividend = Converters.toNumber(first);
26    const divisor = Converters.toNumber(second);
27    if (Numbers.isZero(divisor)) {
28      throw new DivException("Formula DIVIDE parameter 2 cannot be zero.");
29    }
30    return ROUNDDOWN.SELF.run(null, dividend / divisor);
31  }
32}