1- //
2- // Percent.swift
3- // Units
4- //
5- // Created by Jason Jobe on 9/5/25.
6- //
7-
81import Foundation
92/*
103 NOTE: Should consider introducing `protocol Scalar`
@@ -72,17 +65,22 @@ public struct Percent: Numeric, Equatable, Codable {
7265 }
7366}
7467
68+ extension Measurement {
69+ public var isPercent : Bool {
70+ self . unit == Percent . unit
71+ }
72+
73+ public var asPercent : Percent ? {
74+ isPercent ? Percent ( magnitude: self . value/ 100 ) : nil
75+ }
76+ }
77+
7578// MARK: Percent as Unit
7679extension Percent {
7780 public var unit : Unit { Self . unit }
7881
7982 public static let unit = Unit (
80- definedBy: try ! DefinedUnit (
81- name: " percent " ,
82- symbol: " % " ,
83- dimension: [ : ] ,
84- coefficient: 0.01
85- ) )
83+ definedBy: DefaultUnits . percent)
8684}
8785
8886// MARK: Numeric Conformance
@@ -129,11 +127,11 @@ extension BinaryFloatingPoint {
129127// AdditiveArithmetic operations `*` and `/`
130128
131129public extension Measurement {
132- /// Calculate the percentage of the Measurement
130+ /// Adds a percentage to a measurement by increasing its value by the given percent.
133131 /// - Parameters:
134- /// - lhs: The left-hand-side measurement
135- /// - rhs: The right-hand-side measurement
136- /// - Returns: A new measurement with the summed scalar values and the same unit of measure
132+ /// - lhs: The base measurement.
133+ /// - rhs: The percentage to add.
134+ /// - Returns: A new `Measurement` with its value increased by the given percentage.
137135 @_disfavoredOverload
138136 static func + ( lhs: Measurement , rhs: Percent ) -> Measurement {
139137 return Measurement (
@@ -142,6 +140,11 @@ public extension Measurement {
142140 )
143141 }
144142
143+ /// Subtracts a percentage from a measurement by decreasing its value by the given percent.
144+ /// - Parameters:
145+ /// - lhs: The base measurement.
146+ /// - rhs: The percentage to subtract.
147+ /// - Returns: A new `Measurement` with its value decreased by the given percentage.
145148 @_disfavoredOverload
146149 static func - ( lhs: Measurement , rhs: Percent ) -> Measurement {
147150 return Measurement (
@@ -150,35 +153,51 @@ public extension Measurement {
150153 )
151154 }
152155
156+ /// Increases a measurement in place by the given percentage.
157+ /// - Parameters:
158+ /// - lhs: The measurement to modify.
159+ /// - rhs: The percentage to add.
153160 @_disfavoredOverload
154161 static func += ( lhs: inout Measurement , rhs: Percent ) {
155162 lhs = lhs + rhs
156163 }
157-
164+
165+ /// Decreases a measurement in place by the given percentage.
166+ /// - Parameters:
167+ /// - lhs: The measurement to modify.
168+ /// - rhs: The percentage to subtract.
158169 @_disfavoredOverload
159170 static func -= ( lhs: inout Measurement , rhs: Percent ) {
160171 lhs = lhs - rhs
161172 }
162-
173+
163174}
164175
165176// Scalar operations `*` and `/`
166177public extension Measurement {
167-
178+ /// Multiplies a measurement by a percentage, treating the percent as a scalar (e.g., 25% = 0.25).
179+ /// - Parameters:
180+ /// - lhs: The base measurement to scale.
181+ /// - rhs: The percentage factor.
182+ /// - Returns: A new `Measurement` whose value is `lhs.value * rhs.magnitude` with the same unit.
168183 @_disfavoredOverload
169184 static func * ( lhs: Measurement , rhs: Percent ) -> Measurement {
170185 return Measurement (
171186 value: lhs. value * rhs. magnitude,
172187 unit: lhs. unit
173188 )
174189 }
175-
190+
191+ /// Divides a measurement by a percentage, treating the percent as a scalar (e.g., 25% = 0.25).
192+ /// - Parameters:
193+ /// - lhs: The base measurement to scale.
194+ /// - rhs: The percentage divisor.
195+ /// - Returns: A new `Measurement` whose value is `lhs.value / rhs.magnitude` with the same unit.
176196 @_disfavoredOverload
177197 static func / ( lhs: Measurement , rhs: Percent ) -> Measurement {
178198 return Measurement (
179199 value: lhs. value / rhs. magnitude,
180200 unit: lhs. unit
181201 )
182202 }
183-
184203}
0 commit comments