@kikuchan/decimal 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 kikuchan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,219 @@
1
+ # @kikuchan/decimal
2
+
3
+ Arbitrary precision decimal arithmetic for TypeScript and JavaScript. Avoids binary floating-point rounding errors.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @kikuchan/decimal
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```ts
14
+ import { Decimal } from '@kikuchan/decimal';
15
+
16
+ const price = Decimal('12.345');
17
+ const quantity = Decimal(3);
18
+ const total = price.mul(quantity).round(2);
19
+
20
+ console.log(total.toString()); // "37.04"
21
+ ```
22
+
23
+ ## Creating Decimals
24
+
25
+ The `Decimal()` constructor accepts numbers, strings, bigints, or existing Decimal instances:
26
+
27
+ ```ts
28
+ Decimal(100) // from number
29
+ Decimal('12.345') // from string
30
+ Decimal('1.5e2') // scientific notation
31
+ Decimal(12345n) // from bigint
32
+ Decimal(existing) // from another Decimal
33
+
34
+ // Nullable values pass through unchanged
35
+ Decimal(null) // returns null
36
+ Decimal(undefined) // returns undefined
37
+ ```
38
+
39
+ ## API Overview
40
+
41
+ ### Arithmetic Operations
42
+
43
+ All operations are **immutable by default** and return new Decimal instances:
44
+
45
+ ```ts
46
+ const a = Decimal('10.5');
47
+ const b = Decimal('2.3');
48
+
49
+ a.add(b) // addition
50
+ a.sub(b) // subtraction
51
+ a.mul(b) // multiplication
52
+ a.div(b) // division (default 18 decimal places)
53
+ a.div(b, 5) // division with custom precision
54
+
55
+ a.mod(b) // modulo (same sign rules as JavaScript %)
56
+ a.modPositive(b) // always non-negative remainder
57
+ ```
58
+
59
+ ### Mutable Operations
60
+
61
+ Methods ending with `$` modify the value in place and return `this`:
62
+
63
+ ```ts
64
+ const value = Decimal('1.2345');
65
+ value.round$(2); // value is now 1.23
66
+
67
+ // Both styles support chaining
68
+ value.add$(1).mul$(2).round$(0);
69
+ Decimal('10').add(5).mul(2).round(0); // 30
70
+ ```
71
+
72
+ ### Rounding & Precision
73
+
74
+ ```ts
75
+ const num = Decimal('12.3456');
76
+
77
+ num.round(2) // 12.35 (half away from zero)
78
+ num.floor(2) // 12.34
79
+ num.ceil(2) // 12.35
80
+ num.trunc(2) // 12.34
81
+
82
+ // Negative precision rounds to powers of 10
83
+ Decimal('1234').round(-2) // 1200 (nearest hundred)
84
+
85
+ // Round to specific step sizes
86
+ Decimal('12.7').roundBy('0.5') // 13.0
87
+ Decimal('47').roundBy(10) // 50
88
+ ```
89
+
90
+ **Precision control:**
91
+ - Positive values: digits after decimal point
92
+ - Zero: round to whole units
93
+ - Negative values: powers of ten (e.g., `-2` = hundreds)
94
+
95
+ ### Sign & Absolute Value
96
+
97
+ ```ts
98
+ const num = Decimal('-5.5');
99
+
100
+ num.neg() // 5.5
101
+ num.abs() // 5.5
102
+ num.isNegative() // true
103
+ num.isPositive() // false
104
+ num.isZero() // false
105
+ ```
106
+
107
+ ### Comparison
108
+
109
+ ```ts
110
+ const a = Decimal('10');
111
+ const b = Decimal('20');
112
+
113
+ a.cmp(b) // -1 (less), 0 (equal), or 1 (greater)
114
+ a.eq(b) // false
115
+ a.lt(b) // true
116
+ a.le(b) // true
117
+ a.gt(b) // false
118
+ a.ge(b) // false
119
+
120
+ a.between(5, 15) // true
121
+ a.between(15, 25) // false
122
+ a.isCloseTo(10.001, 0.01) // true
123
+ ```
124
+
125
+ ### Advanced Math
126
+
127
+ ```ts
128
+ const num = Decimal('100');
129
+
130
+ num.sqrt() // square root
131
+ num.root(3) // cube root
132
+ num.pow(2) // exponentiation
133
+ num.pow('0.5') // fractional exponents (non-negative bases only)
134
+ num.log(10) // logarithm (base 10)
135
+ num.inverse() // 1 / num
136
+
137
+ // All accept optional precision argument
138
+ num.sqrt(10) // 10 decimal places
139
+ num.log(10, 20) // 20 decimal places
140
+ ```
141
+
142
+ ### Decimal Point Manipulation
143
+
144
+ ```ts
145
+ const num = Decimal('12.345');
146
+
147
+ num.shift10(2) // 1234.5 (shift right = multiply by 10²)
148
+ num.shift10(-1) // 1.2345 (shift left = divide by 10)
149
+
150
+ // Split into integer and fractional parts
151
+ const [whole, frac] = num.split(2); // [12.34, 0.005]
152
+ ```
153
+
154
+ ### Conversion
155
+
156
+ ```ts
157
+ const num = Decimal('12.345');
158
+
159
+ num.toString() // "12.345"
160
+ num.toFixed(2) // "12.35"
161
+ num.number() // 12.345 (native number)
162
+ num.integer() // 12n (bigint, truncated)
163
+ ```
164
+
165
+ ### Utilities
166
+
167
+ ```ts
168
+ Decimal('12.3000').rescale() // "12.3" (remove trailing zeros)
169
+
170
+ const a = Decimal('5.5');
171
+ a.clamp(0, 10) // 5.5 (bounded)
172
+ a.clamp(6, 10) // 6 (clamped to minimum)
173
+
174
+ // Exported utility functions
175
+ import { min, max, minmax, pow10, isDecimal } from '@kikuchan/decimal';
176
+
177
+ min(1, 2, 3) // Decimal(1)
178
+ max('1.5', '2.5', '3.5') // Decimal(3.5)
179
+ minmax(5, 1, 3) // [Decimal(1), Decimal(5)]
180
+ pow10(3) // Decimal(1000)
181
+ isDecimal(value) // type guard
182
+ ```
183
+
184
+ ## Rounding Modes
185
+
186
+ All rounding operations support these modes:
187
+
188
+ - `'round'` — half away from zero (default)
189
+ - `'floor'` — toward negative infinity
190
+ - `'ceil'` — toward positive infinity
191
+ - `'trunc'` — toward zero
192
+
193
+ ```ts
194
+ const num = Decimal('12.75');
195
+ num.round(1, 'round') // 12.8
196
+ num.round(1, 'floor') // 12.7
197
+ num.round(1, 'ceil') // 12.8
198
+ num.round(1, 'trunc') // 12.7
199
+ ```
200
+
201
+ ## Important Notes
202
+
203
+ - **Immutability**: Operations return new instances unless using `$` methods
204
+ - **Precision**: Division and advanced math default to 18 decimal places
205
+ - **No silent overflow**: All operations maintain arbitrary precision
206
+ - **Null-safe**: `Decimal(null)` and `Decimal(undefined)` pass through unchanged
207
+
208
+ ## Development
209
+
210
+ ```bash
211
+ pnpm test # Run tests
212
+ pnpm build # Build package
213
+ pnpm lint # Lint source
214
+ pnpm format # Check formatting
215
+ ```
216
+
217
+ ## License
218
+
219
+ See LICENSE file for details.
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@kikuchan/decimal",
3
+ "description": "Arbitrary precision decimal arithmetic in TypeScript by leveraging native BigInt",
4
+ "keywords": [
5
+ "decimal",
6
+ "arbitrary",
7
+ "bigint"
8
+ ],
9
+ "version": "0.1.0-alpha.1",
10
+ "type": "module",
11
+ "main": "./src/index.ts",
12
+ "author": "kikuchan <kikuchan98@gmail.com>",
13
+ "homepage": "https://github.com/kikuchan/utils-on-npm#readme",
14
+ "license": "MIT",
15
+ "scripts": {
16
+ "build": "tsdown",
17
+ "type:check": "tsc --noEmit"
18
+ }
19
+ }