Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/Units/Registry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,9 @@ class Registry {
DefaultUnits.imperialGallon,
DefaultUnits.imperialPeck,
DefaultUnits.metricCup,

// MARK: Unitless

DefaultUnits.percent
]
}
9 changes: 9 additions & 0 deletions Sources/Units/Unit/DefaultUnits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1271,4 +1271,13 @@ enum DefaultUnits {
dimension: [.Length: 3],
coefficient: 0.00025
)

// MARK: Unitless

static let percent = try! DefinedUnit(
name: "percent",
symbol: "%",
dimension: [:],
coefficient: 0.01
)
}
4 changes: 4 additions & 0 deletions Sources/Units/Unit/Unit+DefaultUnits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,8 @@ public extension Unit {
static let imperialGallon = Unit(definedBy: DefaultUnits.imperialGallon)
static let imperialPeck = Unit(definedBy: DefaultUnits.imperialPeck)
static let metricCup = Unit(definedBy: DefaultUnits.metricCup)

// MARK: Unitless

static let percent = Unit(definedBy: DefaultUnits.percent)
}
58 changes: 58 additions & 0 deletions Tests/UnitsTests/MeasurementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,64 @@ final class MeasurementTests: XCTestCase {
)
}

func testPercent() throws {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an additional test in the DefinitionsTests.swift and copy the formatting there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see that file?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XCTAssertEqual(
1.measured(in: .percent),
1.measured(in: .percent)
)

XCTAssertNotEqual(
100.measured(in: .percent),
1.measured(in: .none)
)

try {
let percent1 = 5.measured(in: .percent)
let percent2 = 5.measured(in: .percent)
XCTAssertEqual(
try percent1 + percent2,
10.measured(in: .percent),
accuracy: accuracy
)
}()

try {
let percent1 = 5.measured(in: .percent)
let value2 = 5.measured(in: .none)
XCTAssertThrowsError(try percent1 + value2)
Copy link
Owner

@NeedleInAJayStack NeedleInAJayStack Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, looks like this test is failing in CI because this no longer throws an error. I've added cross-unit addition/subtraction support since this MR was opened, so this will succeed and result in 505%.

}()

XCTAssertTrue(
2.measured(in: .percent).isDimensionallyEquivalent(
to: 2.measured(in: .percent)
)
)

XCTAssertTrue(
2.measured(in: .percent).isDimensionallyEquivalent(
to: 2.measured(in: .none)
)
)

XCTAssertEqual(
try 1.measured(in: .percent).convert(to: .none),
0.01.measured(in: .none),
accuracy: accuracy
)

XCTAssertEqual(
try 1.measured(in: .none).convert(to: .percent),
100.measured(in: .percent),
accuracy: accuracy
)

_ = {
let measurement = 1.measured(in: .percent) / 2.measured(in: .second)
XCTAssertEqual(measurement.unit, Unit.percent / Unit.second)
XCTAssertEqual(measurement.value, 0.5, accuracy: accuracy)
}()
}

func testComplexArithmetic() throws {
XCTAssertEqual(
try 1.measured(in: .mile / .mile) * 1.measured(in: .mile) - 1.measured(in: .mile),
Expand Down
Loading