Skip to content

Commit 2060fbb

Browse files
committed
Add toString() options and clamping methods to Money docs
1 parent a732395 commit 2060fbb

3 files changed

Lines changed: 89 additions & 6 deletions

File tree

apps/docs/content/docs/ai-guide.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: AI Reference
33
description: Compact reference for AI code generation
44
---
55

6-
> This page is optimized for AI assistants. For detailed documentation, see [Getting Started](/docs/getting-started).
6+
**NOTE:** This page is optimized for AI assistants. For detailed documentation, see [Getting Started](/docs/getting-started).
77

88
## Installation
99

@@ -45,9 +45,9 @@ Money(100.5) // Works but warns about precision loss
4545
const price = Money("$1234.56")
4646

4747
price.toString() // "$1,234.56"
48-
price.toString({ symbol: false }) // "1,234.56"
49-
price.toString({ grouping: false }) // "$1234.56"
50-
price.toString({ decimals: 0 }) // "$1,235"
48+
price.toString({ excludeCurrency: true }) // "1,234.56"
49+
price.toString({ compact: true }) // "$1K"
50+
price.toString({ maxDecimals: 0 }) // "$1,235"
5151
price.toString({ locale: "de-DE" }) // "1.234,56 $"
5252
```
5353

apps/docs/content/docs/api/fixed-point.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: FixedPoint & Rational
33
description: Arbitrary-precision numeric types for financial calculations
44
---
55

6-
> **Note:** Most users won't need these types directly. The `Money` class handles precision automatically. These are useful for advanced calculations or when you need raw numeric operations without currency semantics.
6+
**Note:** Most users won't need these types directly. The `Money` class handles precision automatically. These are useful for advanced calculations or when you need raw numeric operations without currency semantics.
77

88
cent provides two numeric types beyond `Money` for precise mathematical operations.
99

apps/docs/content/docs/api/money.mdx

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,59 @@ const cents = amount.roundTo(2, Round.HALF_EVEN)
229229
const whole = amount.roundTo(0, Round.FLOOR)
230230
```
231231

232+
## Clamping Methods
233+
234+
### clamp(min, max)
235+
236+
Constrain a value within a range.
237+
238+
```typescript
239+
Money("$50").clamp("$0", "$100") // $50.00 (within bounds)
240+
Money("-$50").clamp("$0", "$100") // $0.00 (below min, clamped up)
241+
Money("$150").clamp("$0", "$100") // $100.00 (above max, clamped down)
242+
243+
// With Money instances
244+
const min = Money("$10")
245+
const max = Money("$1000")
246+
price.clamp(min, max)
247+
248+
// With numbers (interpreted in same currency)
249+
price.clamp(0, 100)
250+
```
251+
252+
### atLeast(min)
253+
254+
Ensure a minimum value.
255+
256+
```typescript
257+
Money("$50").atLeast("$0") // $50.00 (already above min)
258+
Money("-$50").atLeast("$0") // $0.00 (raised to min)
259+
260+
// Ensure non-negative amounts
261+
const safeAmount = amount.atLeast(0)
262+
263+
// With Money instance
264+
Money("$25").atLeast(Money("$50")) // $50.00
265+
```
266+
267+
### atMost(max)
268+
269+
Ensure a maximum value.
270+
271+
```typescript
272+
Money("$50").atMost("$100") // $50.00 (already below max)
273+
Money("$150").atMost("$100") // $100.00 (reduced to max)
274+
275+
// Cap at maximum allowed amount
276+
const cappedAmount = amount.atMost("$10000")
277+
278+
// With Money instance
279+
Money("$75").atMost(Money("$50")) // $50.00
280+
```
281+
232282
## Formatting Methods
233283

234-
### toString()
284+
### toString(options?)
235285

236286
Format as a string with currency symbol.
237287

@@ -240,6 +290,39 @@ Money("$100.50").toString() // "$100.50"
240290
Money("0.5 BTC").toString() // "0.50000000 BTC"
241291
```
242292

293+
**Options:**
294+
295+
```typescript
296+
const price = Money("$1234.567")
297+
298+
// Locale formatting
299+
price.toString({ locale: "de-DE" }) // "1.234,57 $"
300+
301+
// Compact notation
302+
Money("$1500000").toString({ compact: true }) // "$1.5M"
303+
304+
// Control decimal places
305+
price.toString({ maxDecimals: 2 }) // "$1,234.57"
306+
price.toString({ minDecimals: 4 }) // "$1,234.5670"
307+
308+
// Exclude currency symbol
309+
price.toString({ excludeCurrency: true }) // "1,234.567"
310+
311+
// Rounding mode for formatting
312+
price.toString({ maxDecimals: 2, roundingMode: Round.FLOOR }) // "$1,234.56"
313+
```
314+
315+
| Option | Type | Description |
316+
|--------|------|-------------|
317+
| `locale` | `string` | Display locale (default "en-US") |
318+
| `compact` | `boolean` | Use compact notation (e.g., $1M) |
319+
| `maxDecimals` | `number` | Maximum decimal places to display |
320+
| `minDecimals` | `number` | Minimum decimal places (forces trailing zeros) |
321+
| `excludeCurrency` | `boolean` | Exclude currency symbol/code |
322+
| `preferSymbol` | `boolean` | Prefer symbol over code for non-ISO currencies |
323+
| `preferredUnit` | `string` | Preferred fractional unit (e.g., "sat" for BTC) |
324+
| `roundingMode` | `RoundingMode` | Rounding mode for formatting |
325+
243326
### toJSON()
244327

245328
Return JSON-serializable representation.

0 commit comments

Comments
 (0)