Skip to content

splitIntegerDecimal(x)

拆分整数与小数部分, 还可以配合toNumberStr使用

入参

参数类型必填说明
xnumber | string传递非数字字符串时,直接返回原值

返回

ts
[string, string]; // [ 整数部分,小数部分 ]

示例

ts
import { splitIntegerDecimal } from "./splitIntegerDecimal";
import { toNumberStr } from "../toNumberStr";

describe("splitIntegerDecimal.ts", () => {
  test("合法数字", () => {
    expect(splitIntegerDecimal("1")).toEqual(["1", ""]);
    expect(splitIntegerDecimal("100.00")).toEqual(["100", "00"]);
    expect(splitIntegerDecimal("100.39")).toEqual(["100", "39"]);
    expect(splitIntegerDecimal("100.000")).toEqual(["100", "000"]);
    expect(splitIntegerDecimal("-100.000")).toEqual(["-100", "000"]);
  });
  test("配合toNumberStr", () => {
    expect(splitIntegerDecimal(toNumberStr("-100.000"))).toEqual(["-100", ""]);
    expect(splitIntegerDecimal(toNumberStr("-100."))).toEqual(["-100", ""]);
    expect(splitIntegerDecimal(toNumberStr("-.01"))).toEqual(["-0", "01"]);
  });
  test("splitIntegerDecimal", () => {
    expect(splitIntegerDecimal("100.000.000")).toEqual(["100.000.000", ""]);
    expect(splitIntegerDecimal("100,000.000")).toEqual(["100,000", "000"]);
    expect(splitIntegerDecimal("100,000.37")).toEqual(["100,000", "37"]);
    expect(splitIntegerDecimal(".123")).toEqual(["", "123"]);
    expect(splitIntegerDecimal(".12")).toEqual(["", "12"]);
    expect(splitIntegerDecimal("1.12")).toEqual(["1", "12"]);
    expect(splitIntegerDecimal("1e12.12")).toEqual(["1e12", "12"]);
    expect(splitIntegerDecimal(100.0)).toEqual(["100", ""]);
    expect(splitIntegerDecimal("10,000.ab")).toEqual(["10,000.ab", ""]);
    expect(splitIntegerDecimal(null as unknown as any)).toEqual([null, ""]);
  });
});