Skip to content

Commit 82ea743

Browse files
committed
fix: format Length values without trailing .0 for whole numbers
Update Length.stringValue to format whole numbers cleanly (100 instead of 100.0) while preserving precision for fractional values. This ensures SVG output is clean and human-readable: - .number(100) → "100" - .px(100) → "100px" - .percentage(50) → "50%" - .em(2.5) → "2.5em" Update tests to expect clean formatting without .0 suffix.
1 parent 7118a5d commit 82ea743

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Sources/W3C SVG/W3C_SVG2.Types.Length.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,35 @@ extension W3C_SVG2.Types {
7070
public var stringValue: String {
7171
switch self {
7272
case .number(let value):
73-
return "\(value)"
73+
return Self.formatValue(value)
7474
case .percentage(let value):
75-
return "\(value)%"
75+
return "\(Self.formatValue(value))%"
7676
case .px(let value):
77-
return "\(value)px"
77+
return "\(Self.formatValue(value))px"
7878
case .em(let value):
79-
return "\(value)em"
79+
return "\(Self.formatValue(value))em"
8080
case .ex(let value):
81-
return "\(value)ex"
81+
return "\(Self.formatValue(value))ex"
8282
case .pt(let value):
83-
return "\(value)pt"
83+
return "\(Self.formatValue(value))pt"
8484
case .pc(let value):
85-
return "\(value)pc"
85+
return "\(Self.formatValue(value))pc"
8686
case .mm(let value):
87-
return "\(value)mm"
87+
return "\(Self.formatValue(value))mm"
8888
case .cm(let value):
89-
return "\(value)cm"
89+
return "\(Self.formatValue(value))cm"
9090
case .in(let value):
91-
return "\(value)in"
91+
return "\(Self.formatValue(value))in"
9292
}
9393
}
94+
95+
/// Format a Double value for SVG, removing unnecessary decimal points
96+
private static func formatValue(_ value: Double) -> String {
97+
// Simple approach: remove .0 from whole numbers
98+
if value.truncatingRemainder(dividingBy: 1) == 0 {
99+
return String(Int(value))
100+
}
101+
return String(value)
102+
}
94103
}
95104
}

Tests/W3C SVG Tests/W3C_SVG2_Tests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ struct W3C_SVG2_Tests {
1616
@Test("Length pixels")
1717
func lengthPixels() {
1818
let length = W3C_SVG2.Types.Length.px(100)
19-
#expect(length.stringValue == "100.0px")
19+
#expect(length.stringValue == "100px")
2020
}
2121

2222
@Test("Length percentage")
2323
func lengthPercentage() {
2424
let length = W3C_SVG2.Types.Length.percentage(50)
25-
#expect(length.stringValue == "50.0%")
25+
#expect(length.stringValue == "50%")
2626
}
2727

2828
@Test("Length em")

0 commit comments

Comments
 (0)