Duration.UnitsFormatStyle.FractionalPartDisplayStrategy
A strategy that determines how to format the fractional part of a duration if the allowed units can’t represent it exactly.
Declaration
struct FractionalPartDisplayStrategyOverview
When using a Duration.UnitsFormatStyle, specifying a FractionalPartDisplayStrategy enables you to decide how to balance between accuracy and verbosity when you’re not using all of the available units (hours, minutes, and seconds). When a formatted duration has a fractional part, you can hide it entirely, round the unit up or down while hiding the fractional part, or show the unit with a fraction.
The following example shows different display strategies used with a duration of 1 hour, 15 minutes and unit format styles that only show hours.
let duration = Duration.seconds(75 * 60) // 1 minute, 15 seconds
let hide = duration.formatted(
.units(allowed: [.hours],
width: .wide,
fractionalPart: .hide)) // 1 hour
let hideRounded = duration.formatted(
.units(allowed: [.hours],
width: .wide,
fractionalPart: .hide(rounded:.up))) // 2 hours
let show = duration.formatted(
.units(allowed: [.hours],
width: .wide,
fractionalPart: .show(length: 2))) // 1.25 hours