Skip to content

Commit 1a235c1

Browse files
authored
feature: number-formatting
* feat: Add basic number formatting with tests * style: Fix SwiftLint warnings in number formatting * style: Fix multiline parameter brackets formatting * style: Fix multiline parameter brackets to match SwiftLint rules * chore: Disable multiline_parameters_brackets rule to resolve SwiftFormat conflict * style: Fix opening brace placement to match SwiftLint rules * chore: Disable opening_brace rule to resolve SwiftFormat conflict * feat: Add core number formatting protocol with decimal support * feat: Add percentage formatting support * feat: Add currency formatting support * feat: Add scientific notation formatting support * feat: Add ordinal number formatting support * feat: Add number spelling support * feat: Add compact notation support * feat: Add binary formatting support * feat: Add hexadecimal formatting support * feat: Add Roman numeral formatting support * feat: Add octal formatting support * feat: Add custom base-N formatting support * feat: Add accounting format support * feat: Add file size, duration, fraction, and unit formatting * feat: Add file size, duration, fraction, and unit formatting * docs: Add comprehensive documentation for number formatting features * fix: German locale accounting format for currency values * test: verify German locale accounting format * style: fix formatting
1 parent 2db28e8 commit 1a235c1

File tree

5 files changed

+1334
-0
lines changed

5 files changed

+1334
-0
lines changed

.swiftlint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ disabled_rules:
88
- trailing_comma
99
- comment_spacing
1010
- switch_case_alignment
11+
- multiline_parameters_brackets
12+
- opening_brace
1113

1214
opt_in_rules:
1315
- array_init
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// NumberFormatting+Numeric.swift
2+
// SwiftDevKit
3+
//
4+
// Copyright (c) 2025 owdax and The SwiftDevKit Contributors
5+
// MIT License - https://opensource.org/licenses/MIT
6+
7+
import Foundation
8+
9+
// MARK: - Numeric Types Conformance
10+
11+
extension Int: NumberFormattable {
12+
public func formatted(
13+
decimals: Int? = nil,
14+
grouping: Bool? = nil,
15+
roundingRule: NumberFormatter.RoundingMode? = nil) throws -> String
16+
{
17+
let formatter = NumberFormatter()
18+
formatter.numberStyle = .decimal
19+
formatter.maximumFractionDigits = decimals ?? 0
20+
formatter.usesGroupingSeparator = grouping ?? true
21+
formatter.roundingMode = roundingRule ?? .down
22+
23+
guard let result = formatter.string(from: NSNumber(value: self)) else {
24+
throw NumberFormattingError.invalidNumber(String(self))
25+
}
26+
return result
27+
}
28+
}
29+
30+
extension Double: NumberFormattable {
31+
public func formatted(
32+
decimals: Int? = nil,
33+
grouping: Bool? = nil,
34+
roundingRule: NumberFormatter.RoundingMode? = nil) throws -> String
35+
{
36+
let formatter = NumberFormatter()
37+
formatter.numberStyle = .decimal
38+
formatter.maximumFractionDigits = decimals ?? 2
39+
formatter.minimumFractionDigits = decimals ?? 2
40+
formatter.usesGroupingSeparator = grouping ?? true
41+
formatter.roundingMode = roundingRule ?? .down
42+
43+
guard let result = formatter.string(from: NSNumber(value: self)) else {
44+
throw NumberFormattingError.invalidNumber(String(self))
45+
}
46+
return result
47+
}
48+
}
49+
50+
extension Float: NumberFormattable {
51+
public func formatted(
52+
decimals: Int? = nil,
53+
grouping: Bool? = nil,
54+
roundingRule: NumberFormatter.RoundingMode? = nil) throws -> String
55+
{
56+
let formatter = NumberFormatter()
57+
formatter.numberStyle = .decimal
58+
formatter.maximumFractionDigits = decimals ?? 2
59+
formatter.minimumFractionDigits = decimals ?? 2
60+
formatter.usesGroupingSeparator = grouping ?? true
61+
formatter.roundingMode = roundingRule ?? .down
62+
63+
guard let result = formatter.string(from: NSNumber(value: self)) else {
64+
throw NumberFormattingError.invalidNumber(String(self))
65+
}
66+
return result
67+
}
68+
}

0 commit comments

Comments
 (0)