-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrounding.test.ts
More file actions
137 lines (116 loc) · 4.15 KB
/
rounding.test.ts
File metadata and controls
137 lines (116 loc) · 4.15 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { floor, ceil, truncate, round, roundToPrecision } from "../src/rounding";
describe("floor", () => {
test("should floor positive integers", () => {
expect(floor("5")).toBe("5");
expect(floor("0")).toBe("0");
});
test("should floor positive decimals", () => {
expect(floor("5.1")).toBe("5");
expect(floor("5.9")).toBe("5");
expect(floor("0.1")).toBe("0");
expect(floor("0.9")).toBe("0");
});
test("should floor negative integers", () => {
expect(floor("-5")).toBe("-5");
expect(floor("-0")).toBe("0");
});
test("should floor negative decimals", () => {
expect(floor("-5.1")).toBe("-6");
expect(floor("-5.9")).toBe("-6");
expect(floor("-0.1")).toBe("-1");
expect(floor("-0.9")).toBe("-1");
});
test("should handle large numbers", () => {
expect(floor("999999999999999999999.1")).toBe("999999999999999999999");
expect(floor("-999999999999999999999.1")).toBe("-1000000000000000000000");
});
});
describe("ceil", () => {
test("should ceil positive integers", () => {
expect(ceil("5")).toBe("5");
expect(ceil("0")).toBe("0");
});
test("should ceil positive decimals", () => {
expect(ceil("5.1")).toBe("6");
expect(ceil("5.9")).toBe("6");
expect(ceil("0.1")).toBe("1");
expect(ceil("0.9")).toBe("1");
});
test("should ceil negative integers", () => {
expect(ceil("-5")).toBe("-5");
expect(ceil("-0")).toBe("0");
});
test("should ceil negative decimals", () => {
expect(ceil("-5.1")).toBe("-5");
expect(ceil("-5.9")).toBe("-5");
expect(ceil("-0.1")).toBe("-0");
expect(ceil("-0.9")).toBe("-0");
});
test("should handle large numbers", () => {
expect(ceil("999999999999999999999.1")).toBe("1000000000000000000000");
expect(ceil("-999999999999999999999.1")).toBe("-999999999999999999999");
});
});
describe("truncate", () => {
test("should truncate positive numbers", () => {
expect(truncate("5")).toBe("5");
expect(truncate("5.1")).toBe("5");
expect(truncate("5.9")).toBe("5");
expect(truncate("0.9")).toBe("0");
});
test("should truncate negative numbers", () => {
expect(truncate("-5")).toBe("-5");
expect(truncate("-5.1")).toBe("-5");
expect(truncate("-5.9")).toBe("-5");
expect(truncate("-0.9")).toBe("0");
});
test("should handle large numbers", () => {
expect(truncate("999999999999999999999.123456789")).toBe("999999999999999999999");
expect(truncate("-999999999999999999999.123456789")).toBe("-999999999999999999999");
});
});
describe("round", () => {
test("should round positive numbers", () => {
expect(round("5")).toBe("5");
expect(round("5.4")).toBe("5");
expect(round("5.5")).toBe("6");
expect(round("5.6")).toBe("6");
expect(round("0.4")).toBe("0");
expect(round("0.5")).toBe("1");
});
test("should round negative numbers", () => {
expect(round("-5")).toBe("-5");
expect(round("-5.4")).toBe("-5");
expect(round("-5.5")).toBe("-6");
expect(round("-5.6")).toBe("-6");
expect(round("-0.4")).toBe("0");
expect(round("-0.5")).toBe("-1");
});
test("should handle large numbers", () => {
expect(round("999999999999999999999.4")).toBe("999999999999999999999");
expect(round("999999999999999999999.5")).toBe("1000000000000000000000");
});
});
describe("roundToPrecision", () => {
test("should round to specified precision", () => {
expect(roundToPrecision("5.123", 2)).toBe("5.12");
expect(roundToPrecision("5.126", 2)).toBe("5.13");
expect(roundToPrecision("5.125", 2)).toBe("5.13");
});
test("should handle precision of 0", () => {
expect(roundToPrecision("5.4", 0)).toBe("5");
expect(roundToPrecision("5.5", 0)).toBe("6");
});
test("should pad with zeros when needed", () => {
expect(roundToPrecision("5", 2)).toBe("5.00");
expect(roundToPrecision("5.1", 3)).toBe("5.100");
});
test("should handle negative numbers", () => {
expect(roundToPrecision("-5.123", 2)).toBe("-5.12");
expect(roundToPrecision("-5.126", 2)).toBe("-5.13");
});
test("should remove trailing zeros", () => {
expect(roundToPrecision("5.100", 2)).toBe("5.1");
expect(roundToPrecision("5.000", 2)).toBe("5");
});
});