<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FormatStyle/units(allowed:width:maximumUnitCount:zeroValueUnits:valueLength:fractionalPart:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation11FormatStylePAAs8DurationVAAE05UnitsbC0VRszrlE5units7allowed5width16maximumUnitCount09zeroValueE011valueLength14fractionalPartAGShyAG0J0VG_AG0J5WidthVSiSgAG04ZeromE15DisplayStrategyVAtG010FractionalqtU0VtFZ"
  },
  "title" : "units(allowed:width:maximumUnitCount:zeroValueUnits:valueLength:fractionalPart:)"
}
-->

# units(allowed:width:maximumUnitCount:zeroValueUnits:valueLength:fractionalPart:)

Returns a style for formatting a duration that uses the specified units.

```
static func units(allowed units: Set<Duration.UnitsFormatStyle.Unit> = [.hours, .minutes, .seconds], width: Duration.UnitsFormatStyle.UnitWidth = .abbreviated, maximumUnitCount: Int? = nil, zeroValueUnits: Duration.UnitsFormatStyle.ZeroValueUnitsDisplayStrategy = .hide, valueLength: Int? = nil, fractionalPart: Duration.UnitsFormatStyle.FractionalPartDisplayStrategy = .hide) -> Self
```

## Parameters

`units`

The units that the formatted string may include.

`width`

The width of the unit and the spacing between the value and the unit.

`maximumUnitCount`

The maximum number of duration units, if any, to include in the output string.

`zeroValueUnits`

The strategy for handling leading units with zero values.

`valueLength`

The padding or truncating behavior of the unit value, as an `Int`. Defaults to `nil`, which applies no length limit.

`fractionalPart`

The strategy for displaying a duration if a formatted string can’t represent it exactly with the allowed units.

## Return Value

A duration units format style that uses the specified units.

## Discussion

Use the dot-notation form of this type method when the call point allows the use of <doc://com.apple.documentation/documentation/Swift/Duration/UnitsFormatStyle>. You typically do this when calling the <doc://com.apple.documentation/documentation/Swift/Duration/formatted(_:)> method of <doc://com.apple.documentation/documentation/Swift/Duration>.

The following example creates a duration to represent 1 hour, 10 minutes, 32 seconds, and 400 milliseconds. It then creates a <doc://com.apple.documentation/documentation/Swift/Duration/UnitsFormatStyle> to show the hours, minutes, seconds, and milliseconds parts, with a wide width that presents the full name of each unit.

```swift
let duration = Duration.seconds(70 * 60 + 32) + Duration.milliseconds(400)
let formatted = duration.formatted(
     .units(allowed: [.hours, .minutes, .seconds, .milliseconds],
            width: .wide))
// "1 hour, 10 minutes, 32 seconds, 400 milliseconds"
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)