@kikuchan/bigdate 0.0.1-alpha.0 → 0.0.1-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.
Files changed (2) hide show
  1. package/README.md +1 -219
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,219 +1 @@
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.
1
+ # @kikuchan/bigdate
package/package.json CHANGED
@@ -7,14 +7,14 @@
7
7
  "date",
8
8
  "bigint"
9
9
  ],
10
- "version": "0.0.1-alpha.0",
10
+ "version": "0.0.1-alpha.1",
11
11
  "type": "module",
12
12
  "main": "./index.js",
13
13
  "author": "kikuchan <kikuchan98@gmail.com>",
14
14
  "homepage": "https://github.com/kikuchan/utils-on-npm#readme",
15
15
  "license": "MIT",
16
16
  "peerDependencies": {
17
- "@kikuchan/decimal": "^0.1.0-alpha.5"
17
+ "@kikuchan/decimal": "^0.1.0-alpha.6"
18
18
  },
19
19
  "types": "./index.d.ts",
20
20
  "exports": {