diff --git a/Documentation/Extensions.md b/Documentation/Extensions.md index 52c37bd..b2e0cd9 100644 --- a/Documentation/Extensions.md +++ b/Documentation/Extensions.md @@ -1118,7 +1118,274 @@ false.string // "false" ## Numbers & Geometry - +
+Int — conversions, digits, primes, roman numerals, operators + +### `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` +`0.. 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)` | + +
+ +
+Float / Double + +### `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… +``` + +
+ +
+Decimal + +### `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 +``` + +
+ +
+BinaryInteger + +### `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 +``` + +
+ +
+BinaryFloatingPoint + +### `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 +``` + +
+ +
+SignedNumeric + +### `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" +``` + +
+ +
+CGFloat + +### `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. + +
+ +
+CGRect + +### `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 +``` + +
+ +
+CGSize + +### `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 +``` + +
+ +
+CGPoint / CGRect / CGSize — Hashable + +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 = [] +seen.insert(CGPoint(x: 1, y: 2)) +``` + +
## Date, Data & Files