[docs] Extensions.md: Numbers & Geometry section

This commit is contained in:
Daniel Arantes Loverde
2026-08-29 19:27:31 -03:00
parent c459c9eaf6
commit d16a42fddc

View File

@@ -1118,7 +1118,274 @@ false.string // "false"
## Numbers & Geometry ## Numbers & Geometry
<!-- batch 5 --> <details>
<summary><b>Int</b> — conversions, digits, primes, roman numerals, operators</summary>
### `var double: Double` / `var float: Float` / `var cgFloat: CGFloat` / `var uInt: UInt` / `var uInt32: UInt32` / `var uInt64: UInt64`
Straight numeric conversions (`uInt32`/`uInt64` truncate).
```swift
5.double // 5.0
(-1).uInt32 // 4294967295
```
### `var countableRange: CountableRange<Int>`
`0..<self`.
```swift
3.countableRange // 0..<3
```
### `var degreesToRadians: Double` / `var radiansToDegrees: Double`
Angle conversion.
```swift
180.degreesToRadians // 3.14159
```
### `var digits: [Int]` / `var digitsCount: Int`
Decimal digits of `abs(self)`, and how many.
```swift
1234.digits // [1, 2, 3, 4]
1234.digitsCount // 4
```
### `var kFormatted: String`
Compact "k"/"kk" formatting for values ≥ 1000.
```swift
5300.kFormatted // "5k"
2_500_000.kFormatted // "25kk"
```
### `var timestampToDate: Date`
`Date(timeIntervalSince1970:)`.
```swift
1_700_000_000.timestampToDate // 2023-11-14
```
### `var satsToBTC: String` / `var convertToBTC: String` / `var toBTC: String`
Satoshis → BTC string, 8 decimal places. All three are the same.
```swift
150_000_000.satsToBTC // "1.50000000"
```
### `func isPrime() -> Bool`
Primality test (trial division up to √n).
```swift
7.isPrime() // true
9.isPrime() // false
```
### `func romanNumeral() -> String?`
Roman numerals for positive integers, `nil` for 0 or negative.
```swift
2024.romanNumeral() // "MMXXIV"
```
### `func roundToNearest(_ number: Int) -> Int`
Round to the closest multiple of `number`.
```swift
47.roundToNearest(10) // 50
```
### Operators
| Operator | Meaning | Example |
|---|---|---|
| `a ** b` | exponentiation → `Double` | `2 ** 3``8.0` |
| `√ n` (prefix) | square root → `Double` | `√ 9``3.0` |
| `a ± b` (infix) | `(a+b, a-b)` | `5 ± 3``(8, 2)` |
| `± n` (prefix) | `(n, -n)` | `± 2``(2, -2)` |
</details>
<details>
<summary><b>Float / Double</b></summary>
### `var int: Int` / `var double: Double` (Float) / `var float: Float` (Double) / `var cgFloat: CGFloat`
Numeric conversions.
### `var satsToBTC / convertToBTC / toBTC`
Satoshis → BTC (`Double` here, unlike `Int` which returns a `String`).
```swift
150_000_000.0.satsToBTC // 1.5
```
### `func rounded(toPlaces places: Int) -> Float` — *(Float only)*
Round to N decimal places.
```swift
Float(3.14159).rounded(toPlaces: 2) // 3.14
```
### Operator `a ** b`
Exponentiation, staying in the same type (`Float ** Float → Float`, `Double ** Double → Double`).
```swift
4.4 ** 0.5 // 2.0976
```
</details>
<details>
<summary><b>Decimal</b></summary>
### `mutating func round(_ scale: Int, _ roundingMode:)` / `func rounded(_ scale:, _ roundingMode:) -> Decimal`
Decimal rounding via `NSDecimalRound` — mutating and non-mutating.
```swift
Decimal(2.567).rounded(2, .plain) // 2.57
```
</details>
<details>
<summary><b>BinaryInteger</b></summary>
### `var bytes: [UInt8]`
Big-endian raw byte representation.
```swift
Int16(-128).bytes // [255, 128]
```
### `init?(bytes: [UInt8])`
Reconstruct an integer from bytes (traps if the byte count exceeds the type size;
`nil` if the value doesn't fit exactly).
```swift
Int16(bytes: [0xFF, 0xFD]) // -3
```
</details>
<details>
<summary><b>BinaryFloatingPoint</b></summary>
### `func rounded(numberOfDecimalPlaces: Int, rule: FloatingPointRoundingRule) -> Self`
Round to N places with an explicit rule (negative places treated as 0).
```swift
3.1415927.rounded(numberOfDecimalPlaces: 3, rule: .up) // 3.142
```
</details>
<details>
<summary><b>SignedNumeric</b></summary>
### `var string: String`
`String(describing:)`.
### `var asLocaleCurrency: String?` / `func asCurrency(locale: Locale = pt_BR) -> String?`
Currency formatting in the current locale / a specified locale.
```swift
1234.5.asCurrency() // "R$ 1.234,50"
1234.5.asCurrency(locale: Locale(identifier: "en_US")) // "$1,234.50"
```
### `func spelledOutString(locale: Locale = .current) -> String?`
Number spelled out in words.
```swift
92.spelledOutString(locale: Locale(identifier: "en")) // "ninety-two"
```
</details>
<details>
<summary><b>CGFloat</b></summary>
### `var abs / ceil / floor` / `var int / float / double`
Math and numeric conversions.
```swift
CGFloat(-3.2).abs // 3.2
CGFloat(3.2).ceil // 4.0
```
### `var isPositive: Bool` / `var isNegative: Bool`
Sign checks.
### `var degreesToRadians: CGFloat` / `var radiansToDegrees: CGFloat`
Angle conversion.
</details>
<details>
<summary><b>CGRect</b></summary>
### `var center: CGPoint`
Rect centre.
### `init(center: CGPoint, size: CGSize)`
Build a rect from its centre and size.
```swift
CGRect(center: CGPoint(x: 50, y: 50), size: CGSize(width: 20, height: 10))
// origin (40, 45), size 20×10
```
### `func resizing(to size: CGSize, anchor: CGPoint = (0.5, 0.5)) -> CGRect`
Resize while keeping the given normalised anchor point fixed.
```swift
rect.resizing(to: CGSize(width: 100, height: 100), anchor: CGPoint(x: 0, y: 1))
// grows from the bottom-left corner
```
</details>
<details>
<summary><b>CGSize</b></summary>
### `var aspectRatio: CGFloat` / `var maxDimension: CGFloat` / `var minDimension: CGFloat`
`width / height` (0 when height is 0), and the larger / smaller side.
```swift
CGSize(width: 16, height: 9).aspectRatio // 1.777
```
### `func aspectFit(to boundingSize:) -> CGSize` / `func aspectFill(to boundingSize:) -> CGSize`
Scale to fit inside / fill a bounding size, preserving ratio.
```swift
CGSize(width: 120, height: 80).aspectFit(to: CGSize(width: 100, height: 50))
// 75 × 50
```
### Operators
`+`, `-`, `*` and their `+=`/`-=`/`*=` forms, between two `CGSize`s, a `CGSize`
and a `(width, height)` tuple, or a `CGSize` and a scalar.
```swift
CGSize(width: 5, height: 10) + CGSize(width: 3, height: 4) // 8 × 14
CGSize(width: 5, height: 10) * 3 // 15 × 30
```
</details>
<details>
<summary><b>CGPoint / CGRect / CGSize — <code>Hashable</code></summary>
When SwiftUI is available, `CGPoint`, `CGRect`, and `CGSize` are made
`Hashable` (retroactive conformance) so they can be used as dictionary keys or
in `Set`s and as SwiftUI identifiers.
```swift
var seen: Set<CGPoint> = []
seen.insert(CGPoint(x: 1, y: 2))
```
</details>
## Date, Data & Files ## Date, Data & Files