bigdecimal-string 1.0.0

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.
@@ -0,0 +1,289 @@
1
+ /**
2
+ * BigDecimal - Precise decimal arithmetic for JavaScript
3
+ *
4
+ * Avoids floating-point precision issues by using bigint internally.
5
+ * Supports chainable operations and configurable precision.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const price = new BigDecimal("19.99");
10
+ * const tax = price.multiply(0.08);
11
+ * const total = price.add(tax);
12
+ *
13
+ * // Chaining
14
+ * const result = new BigDecimal("100.00")
15
+ * .subtract("25.50")
16
+ * .multiply(2)
17
+ * .add("10")
18
+ * .toString(); // "159.00"
19
+ * ```
20
+ */
21
+ type BigDecimalInput = string | number | bigint | BigDecimal | null | undefined;
22
+ /**
23
+ * Configuration options for BigDecimal operations
24
+ */
25
+ interface BigDecimalConfig {
26
+ /** Number of decimal places (default: 2) */
27
+ precision?: number;
28
+ /** Rounding mode for division and precision changes */
29
+ roundingMode?: RoundingMode;
30
+ }
31
+ declare enum RoundingMode {
32
+ /** Round towards positive infinity */
33
+ CEILING = "CEILING",
34
+ /** Round towards negative infinity */
35
+ FLOOR = "FLOOR",
36
+ /** Round towards zero (truncate) */
37
+ DOWN = "DOWN",
38
+ /** Round away from zero */
39
+ UP = "UP",
40
+ /** Round to nearest, ties go to even (banker's rounding) */
41
+ HALF_EVEN = "HALF_EVEN",
42
+ /** Round to nearest, ties round up */
43
+ HALF_UP = "HALF_UP",
44
+ /** Round to nearest, ties round down */
45
+ HALF_DOWN = "HALF_DOWN"
46
+ }
47
+ declare class BigDecimal {
48
+ /** Internal representation: value * 10^scale */
49
+ private readonly unscaledValue;
50
+ /** Number of decimal places */
51
+ private readonly scale;
52
+ /**
53
+ * Creates a new BigDecimal instance
54
+ * @param value - The value to create from (string, number, bigint, or another BigDecimal)
55
+ * @param precision - Number of decimal places (default: auto-detected or 2)
56
+ */
57
+ constructor(value?: BigDecimalInput, precision?: number);
58
+ /**
59
+ * Parse a value into unscaled bigint and scale
60
+ */
61
+ private static parse;
62
+ /**
63
+ * Convert scientific notation to plain decimal string
64
+ */
65
+ private static scientificToPlain;
66
+ /**
67
+ * Calculate 10^n as bigint
68
+ */
69
+ private static powerOf10;
70
+ /**
71
+ * Add another value to this BigDecimal
72
+ * @returns A new BigDecimal with the result
73
+ */
74
+ add(other: BigDecimalInput): BigDecimal;
75
+ /**
76
+ * Subtract another value from this BigDecimal
77
+ * @returns A new BigDecimal with the result
78
+ */
79
+ subtract(other: BigDecimalInput): BigDecimal;
80
+ /**
81
+ * Alias for subtract
82
+ */
83
+ minus(other: BigDecimalInput): BigDecimal;
84
+ /**
85
+ * Alias for add
86
+ */
87
+ plus(other: BigDecimalInput): BigDecimal;
88
+ /**
89
+ * Multiply this BigDecimal by another value
90
+ * @returns A new BigDecimal with the result
91
+ */
92
+ multiply(other: BigDecimalInput): BigDecimal;
93
+ /**
94
+ * Alias for multiply
95
+ */
96
+ times(other: BigDecimalInput): BigDecimal;
97
+ /**
98
+ * Divide this BigDecimal by another value
99
+ * @param other - The divisor
100
+ * @param precision - Precision for the result (default: this.scale)
101
+ * @param roundingMode - How to round (default: HALF_UP)
102
+ * @returns A new BigDecimal with the result
103
+ */
104
+ divide(other: BigDecimalInput, precision?: number, roundingMode?: RoundingMode): BigDecimal;
105
+ /**
106
+ * Alias for divide
107
+ */
108
+ dividedBy(other: BigDecimalInput, precision?: number, roundingMode?: RoundingMode): BigDecimal;
109
+ /**
110
+ * Get the remainder of division
111
+ */
112
+ mod(other: BigDecimalInput): BigDecimal;
113
+ /**
114
+ * Compare this BigDecimal to another
115
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
116
+ */
117
+ compareTo(other: BigDecimalInput): -1 | 0 | 1;
118
+ /**
119
+ * Check if this BigDecimal equals another value
120
+ */
121
+ equals(other: BigDecimalInput): boolean;
122
+ /**
123
+ * Alias for equals
124
+ */
125
+ eq(other: BigDecimalInput): boolean;
126
+ /**
127
+ * Check if this BigDecimal is less than another
128
+ */
129
+ lessThan(other: BigDecimalInput): boolean;
130
+ /**
131
+ * Alias for lessThan
132
+ */
133
+ lt(other: BigDecimalInput): boolean;
134
+ /**
135
+ * Check if this BigDecimal is less than or equal to another
136
+ */
137
+ lessThanOrEqual(other: BigDecimalInput): boolean;
138
+ /**
139
+ * Alias for lessThanOrEqual
140
+ */
141
+ lte(other: BigDecimalInput): boolean;
142
+ /**
143
+ * Check if this BigDecimal is greater than another
144
+ */
145
+ greaterThan(other: BigDecimalInput): boolean;
146
+ /**
147
+ * Alias for greaterThan
148
+ */
149
+ gt(other: BigDecimalInput): boolean;
150
+ /**
151
+ * Check if this BigDecimal is greater than or equal to another
152
+ */
153
+ greaterThanOrEqual(other: BigDecimalInput): boolean;
154
+ /**
155
+ * Alias for greaterThanOrEqual
156
+ */
157
+ gte(other: BigDecimalInput): boolean;
158
+ /**
159
+ * Check if this BigDecimal is zero
160
+ */
161
+ isZero(): boolean;
162
+ /**
163
+ * Check if this BigDecimal is positive (> 0)
164
+ */
165
+ isPositive(): boolean;
166
+ /**
167
+ * Check if this BigDecimal is negative (< 0)
168
+ */
169
+ isNegative(): boolean;
170
+ /**
171
+ * Get the absolute value
172
+ * @returns A new BigDecimal with the absolute value
173
+ */
174
+ abs(): BigDecimal;
175
+ /**
176
+ * Negate this BigDecimal
177
+ * @returns A new BigDecimal with the opposite sign
178
+ */
179
+ negate(): BigDecimal;
180
+ /**
181
+ * Get the sign of this BigDecimal
182
+ * @returns -1, 0, or 1
183
+ */
184
+ sign(): -1 | 0 | 1;
185
+ /**
186
+ * Change the scale (number of decimal places)
187
+ */
188
+ setScale(newScale: number, roundingMode?: RoundingMode): BigDecimal;
189
+ /**
190
+ * Get the precision (number of decimal places)
191
+ */
192
+ getPrecision(): number;
193
+ /**
194
+ * Convert to string representation
195
+ * @param options - Formatting options
196
+ * @param options.prettify - Add thousand separators (commas)
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * bd("1234567.89").toString() // "1234567.89"
201
+ * bd("1234567.89").toString({ prettify: true }) // "1,234,567.89"
202
+ * bd("1e15").toString() // "1000000000000000.00"
203
+ * bd("1e15").toString({ prettify: true }) // "1,000,000,000,000,000.00"
204
+ * ```
205
+ */
206
+ toString(options?: {
207
+ prettify?: boolean;
208
+ }): string;
209
+ /**
210
+ * Format as a display string with thousand separators
211
+ * Shorthand for toString({ prettify: true })
212
+ *
213
+ * @example
214
+ * ```ts
215
+ * bd("1234567.89").toFormat() // "1,234,567.89"
216
+ * bd("1e12").toFormat() // "1,000,000,000,000.00"
217
+ * ```
218
+ */
219
+ toFormat(): string;
220
+ /**
221
+ * Add thousand separators to an integer string
222
+ */
223
+ private static addThousandSeparators;
224
+ /**
225
+ * Convert to number (may lose precision for large values)
226
+ * @warning Use with caution - JavaScript numbers have limited precision
227
+ */
228
+ toNumber(): number;
229
+ /**
230
+ * Format with fixed decimal places
231
+ * @param decimals - Number of decimal places
232
+ * @param options - Formatting options
233
+ */
234
+ toFixed(decimals: number, options?: {
235
+ prettify?: boolean;
236
+ }): string;
237
+ /**
238
+ * Get the integer part only
239
+ */
240
+ toInteger(): bigint;
241
+ /**
242
+ * valueOf for implicit conversions
243
+ */
244
+ valueOf(): number;
245
+ /**
246
+ * Create from unscaled value and scale
247
+ */
248
+ private static fromUnscaled;
249
+ /**
250
+ * Create a BigDecimal with value zero
251
+ */
252
+ static zero(precision?: number): BigDecimal;
253
+ /**
254
+ * Create a BigDecimal with value one
255
+ */
256
+ static one(precision?: number): BigDecimal;
257
+ /**
258
+ * Sum multiple values
259
+ */
260
+ static sum(...values: BigDecimalInput[]): BigDecimal;
261
+ /**
262
+ * Get the maximum value
263
+ */
264
+ static max(...values: BigDecimalInput[]): BigDecimal;
265
+ /**
266
+ * Get the minimum value
267
+ */
268
+ static min(...values: BigDecimalInput[]): BigDecimal;
269
+ /**
270
+ * Align two BigDecimals to the same scale
271
+ */
272
+ private static alignScales;
273
+ /**
274
+ * Perform division with rounding
275
+ */
276
+ private static roundDivision;
277
+ }
278
+ /**
279
+ * Shorthand factory function
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * const price = bd("19.99");
284
+ * const total = bd(100).subtract(25).multiply(2);
285
+ * ```
286
+ */
287
+ declare function bd(value?: BigDecimalInput, precision?: number): BigDecimal;
288
+
289
+ export { BigDecimal, type BigDecimalConfig, type BigDecimalInput, RoundingMode, bd, BigDecimal as default };