<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Int/init(clamping:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s17FixedWidthIntegerPsE8clampingxqd___tcSzRd__lufc::SYNTHESIZED::s:Si"
  },
  "title" : "init(clamping:)"
}
-->

# init(clamping:)

Creates a new instance with the representable value that’s closest to the
given integer.

```
init<Other>(clamping source: Other) where Other : BinaryInteger
```

## Parameters

`source`

An integer to convert to this type.

## Discussion

If the value passed as `source` is greater than the maximum representable
value in this type, the result is the type’s `max` value. If `source` is
less than the smallest representable value in this type, the result is
the type’s `min` value.

In this example, `x` is initialized as an `Int8` instance by clamping
`500` to the range `-128...127`, and `y` is initialized as a `UInt`
instance by clamping `-500` to the range `0...UInt.max`.

```
let x = Int8(clamping: 500)
// x == 127
// x == Int8.max

let y = UInt(clamping: -500)
// y == 0
```

---

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)