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/utils/Numbers.ts
-rw-r--r--
950
 1/**
 2 * Static class for number utilities.
 3 */
 4import { isNotNull } from "../common/utils/Types";
 5
 6export class Numbers {
 7  /**
 8   * Roughly taken from java.lang.Double.valueOf().
 9   * This is how we stop javascript from parsing stuff like "10    10" as a 10, and so on.
10   */
11  static NUMBER_REG_EX =
12    /^[\x00-\x20]*[+-]?(([0-9]+)(\.)?(([0-9]+)?)([eE][+-]?([0-9]+))?)|(\.([0-9]+)([eE][+-]?([0-9]+))?)|[\x00-\x20]*$/;
13
14  /**
15   * Is positive or negative zero.
16   * @param value
17   */
18  static isZero(value: number): boolean {
19    return value === 0;
20  }
21
22  /**
23   * Convert the value to a number if it matches our regular expression. If not, return null.
24   * @param value - to possible convert.
25   */
26  static toNumberOrNull(value: string): number | null {
27    const matches = value.match(Numbers.NUMBER_REG_EX);
28    if (isNotNull(matches) && matches.length > 0 && matches[0] !== "") {
29      return parseFloat(value);
30    }
31    return null;
32  }
33}