Standard way to "clamp" a number between two values in Swift
<p>Given:</p>
<pre><code>let a = 4.2
let b = -1.3
let c = 6.4
</code></pre>
<p>I want to know the simplest, Swiftiest way to clamp these values to a given range, say <code>0...5</code>, such that:</p>
<pre><code>a -&gt; 4.2
b -&gt; 0
c -&gt; 5
</code></pre>
<p>I know I can do the following:</p>
<pre><code>let clamped = min(max(a, 0), 5)
</code></pre>
<p>Or something like:</p>
<pre><code>let clamped = (a &lt; 0) ? 0 : ((a &gt; 5) ? 5 : a)
</code></pre>
<p>But I was wondering if there were any other ways to do this in Swift—in particular, I want to know (and document on SO, since there doesn't appear to be a question about clamping numbers in Swift) whether there is anything in the Swift standard library intended specifically for this purpose.</p>
<p>There may not be, and if so, that's also an answer I'll happily accept.</p>