name:
src/main/js/formulas/math/FLOOR.ts
-rw-r--r--
1406
1import { isNotUndefined } from "../../utils/Other";
2import { DivException } from "../../errors/DivException";
3import { NumException } from "../../errors/NumException";
4import { SheetColumnRowKey } from "../../models/common/SheetColumnRowKey";
5import { Complex } from "../../models/common/Types";
6import { Converters } from "../../utils/Converters";
7import { AbstractFormula } from "../AbstractFormula";
8import { FormulaName } from "../FormulaName";
9
10export class FLOOR extends AbstractFormula {
11 static SELF: FLOOR = new FLOOR();
12 NAME = FormulaName.FLOOR;
13
14 internal(origin: SheetColumnRowKey, ...values: Array<Complex>) {
15 AbstractFormula.checkLengthBetween(values.length, 1, 2, this.NAME);
16 const value = Converters.first(this.collateralLookup(origin, values[0]));
17 const places = isNotUndefined(values[1])
18 ? Converters.toNumber(Converters.first(this.collateralLookup(origin, values[1])))
19 : 1;
20 if (Converters.toPositiveZero(places) === 0) {
21 return new DivException("Function FLOOR parameter 2 cannot be zero.");
22 }
23 if (value > 0 && places < 0) {
24 return new NumException("Parameters of FLOOR must have same signs.");
25 }
26 const n = Converters.toNumber(value);
27 // If we're just rounding to integer, just floor it no matter what.
28 if (Converters.toPositiveZero(places) === 1) {
29 return Math.floor(n);
30 }
31 return n - (n % places);
32 }
33}