<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Here is the code I use for percentage myself. (I incorrectly said I use a UInt64… I use a UInt32):<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 128, 0); background-color: rgb(255, 255, 255);" class="">///Represents a percentage with the precision of millionths of 1 (i.e. 4 decimal places: XX.XXXX%). The value is always positive (or zero), but may be greater than 100%</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">struct</span> Percentage {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">fileprivate(set)</span> <span style="color: #0433ff" class="">var</span> millionths:<span style="color: #3495af" class="">UInt32</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">fileprivate</span> <span style="color: #0433ff" class="">init</span>(storage:<span style="color: #3495af" class="">UInt32</span>){</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3495af" class="">millionths</span> = storage</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">var</span> quarter = <span style="color: #3495af" class="">Percentage</span>(storage: 250_000)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">var</span> half = <span style="color: #3495af" class="">Percentage</span>(storage: 500_000)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">var</span> threeQuarters = <span style="color: #3495af" class="">Percentage</span>(storage: 750_000)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">var</span> full = <span style="color: #3495af" class="">Percentage</span>(storage: 1_000_000)</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(millionths:<span style="color: #3495af" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(millionths)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(<span style="color: #0433ff" class="">_</span> double:<span style="color: #3495af" class="">Double</span>, range:<span style="color: #3495af" class="">ClosedRange</span><<span style="color: #3495af" class="">Double</span>> = 0...1) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">if</span> range == 0...1 {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>(double * 1_000_000, 0))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }<span style="color: #0433ff" class="">else</span> <span style="color: #0433ff" class="">if</span> range == 0...100{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>(double * 10_000, 0))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }<span style="color: #0433ff" class="">else</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>((double - range.<span style="color: #3495af" class="">lowerBound</span>)/(range.<span style="color: #3495af" class="">upperBound</span> - range.<span style="color: #3495af" class="">lowerBound</span>),0))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(<span style="color: #0433ff" class="">_</span> num:<span style="color: #3495af" class="">Num</span>, range:<span style="color: #3495af" class="">ClosedRange</span><<span style="color: #3495af" class="">Num</span>> = 0...1) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">if</span> range == 0...1 {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>(num * 1_000_000, 0).<span style="color: #3495af" class="">integerValue</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }<span style="color: #0433ff" class="">else</span> <span style="color: #0433ff" class="">if</span> range == 0...100{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>(num * 10_000, 0).<span style="color: #3495af" class="">integerValue</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }<span style="color: #0433ff" class="">else</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">self</span><span style="color: #000000" class="">.</span>millionths<span style="color: #000000" class=""> = </span>UInt32<span style="color: #000000" class="">(</span>max<span style="color: #000000" class="">((num - range.</span>lowerBound<span style="color: #000000" class="">)/(range.</span>upperBound<span style="color: #000000" class=""> - range.</span>lowerBound<span style="color: #000000" class="">),0).</span>integerValue<span style="color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(<span style="color: #0433ff" class="">_</span> decimal:<span style="color: #3495af" class="">Decimal</span>, range:<span style="color: #3495af" class="">ClosedRange</span><<span style="color: #3495af" class="">Decimal</span>> = 0...1) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">if</span> range == 0...1 {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">NSDecimalNumber</span>(decimal: <span style="color: #3495af" class="">max</span>(decimal * 1_000_000, 0)).<span style="color: #3495af" class="">uint32Value</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }<span style="color: #0433ff" class="">else</span> <span style="color: #0433ff" class="">if</span> range == 0...100{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">NSDecimalNumber</span>(decimal: <span style="color: #3495af" class="">max</span>(decimal * 10_000, 0)).<span style="color: #3495af" class="">uint32Value</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }<span style="color: #0433ff" class="">else</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">let</span> shifted = <span style="color: #3495af" class="">max</span>((decimal - range.<span style="color: #3495af" class="">lowerBound</span>)/(range.<span style="color: #3495af" class="">upperBound</span> - range.<span style="color: #3495af" class="">lowerBound</span>),0)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">self</span><span style="color: #000000" class="">.</span>millionths<span style="color: #000000" class=""> = </span>NSDecimalNumber<span style="color: #000000" class="">(decimal: shifted).</span>uint32Value</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(hundredths:<span style="color: #3495af" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>(hundredths * 10_000, 0))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(thousandths:<span style="color: #3495af" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>(thousandths * 1_000, 0))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> isFull:<span style="color: #3495af" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> >= 1_000_000</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> doubleValue:<span style="color: #3495af" class="">Double</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Double</span>(<span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span>)/1_000_000</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> cgfloatValue:<span style="color: #3495af" class="">CGFloat</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">CGFloat</span>(<span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span>)/1_000_000</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> decimalValue:<span style="color: #3495af" class="">Decimal</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Decimal</span>(<span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span>)/1_000_000</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> numValue:<span style="color: #3495af" class="">Num</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Num</span>(numerator: <span style="color: #3495af" class="">Int32</span>(<span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span>), denominator: 1_000_000)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> hundredths:<span style="color: #3495af" class="">Int</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Int</span>(<span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span>/10_000)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> thousandths:<span style="color: #3495af" class="">Int</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Int</span>(<span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span>/1_000)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> tenThousandths:<span style="color: #3495af" class="">Int</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Int</span>(<span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span>/100)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">func</span> map(to range:<span style="color: #3495af" class="">ClosedRange</span><<span style="color: #3495af" class="">Num</span>>) -> <span style="color: #3495af" class="">Num</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">numValue</span> * (range.<span style="color: #3495af" class="">upperBound</span> - range.<span style="color: #3495af" class="">lowerBound</span>) + range.<span style="color: #3495af" class="">lowerBound</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">mutating</span> <span style="color: #0433ff" class="">func</span> clip() {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">if</span> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> > 1_000_000 {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = 1_000_000</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">func</span> clipped()-><span style="color: #3495af" class="">Percentage</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">if</span> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> > 1_000_000 {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Percentage</span>.<span style="color: #3495af" class="">full</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>return<span style="color: #000000" class=""> </span>self</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">extension</span><span style="color: #000000" class=""> </span>Percentage<span style="color: #000000" class="">:</span>CustomStringConvertible<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> description: <span style="color: #3495af" class="">String</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">let</span> num = <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">numValue</span> * 100</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">if</span> num.<span style="color: #3495af" class="">isInteger</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #b4261a" class="">"</span>\<span style="color: #b4261a" class="">(</span>num<span style="color: #b4261a" class="">)%"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #b4261a" class="">"</span>\<span style="color: #b4261a" class="">(</span>num.<span style="color: #3495af" class="">decimalValue</span><span style="color: #b4261a" class="">)%"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">extension</span><span style="color: #000000" class=""> </span>Percentage<span style="color: #000000" class="">:</span>ExpressibleByIntegerLiteral<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(integerLiteral value: <span style="color: #3495af" class="">IntegerLiteralType</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span>.<span style="color: #3495af" class="">millionths</span> = <span style="color: #3495af" class="">UInt32</span>(<span style="color: #3495af" class="">max</span>(value * 10_000, 0))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">extension</span><span style="color: #000000" class=""> </span>Percentage<span style="color: #000000" class="">:</span>Hashable<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> hashValue: <span style="color: #3495af" class="">Int</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">return</span><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">self</span><span style="color: #000000" class="">.</span>millionths<span style="color: #000000" class="">.</span>hashValue</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">func</span> == (lhs:<span style="color: #3495af" class="">Percentage</span>, rhs:<span style="color: #3495af" class="">Percentage</span>)-><span style="color: #3495af" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs.<span style="color: #3495af" class="">millionths</span> == rhs.<span style="color: #3495af" class="">millionths</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">extension</span><span style="color: #000000" class=""> </span>Percentage<span style="color: #000000" class="">:</span>Comparable<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">func</span> < (lhs:<span style="color: #3495af" class="">Percentage</span>, rhs:<span style="color: #3495af" class="">Percentage</span>) -> <span style="color: #3495af" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs.<span style="color: #3495af" class="">millionths</span> < rhs.<span style="color: #3495af" class="">millionths</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">extension</span><span style="color: #000000" class=""> </span>Percentage<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">static</span><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">func</span><span style="color: #000000" class=""> + (lhs:</span>Percentage<span style="color: #000000" class="">, rhs:</span>Percentage<span style="color: #000000" class="">)-></span>Percentage<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Percentage</span>(storage: lhs.<span style="color: #3495af" class="">millionths</span> + rhs.<span style="color: #3495af" class="">millionths</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">static</span><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">func</span><span style="color: #000000" class=""> - (lhs:</span>Percentage<span style="color: #000000" class="">, rhs:</span>Percentage<span style="color: #000000" class="">)-></span>Percentage<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">if</span> rhs > lhs {<span style="color: #0433ff" class="">return</span> 0}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">Percentage</span>(storage: lhs.<span style="color: #3495af" class="">millionths</span> - rhs.<span style="color: #3495af" class="">millionths</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">func</span> * (lhs:<span style="color: #3495af" class="">Percentage</span>, rhs:<span style="color: #3495af" class="">Double</span>)-><span style="color: #3495af" class="">Double</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs.<span style="color: #3495af" class="">doubleValue</span> * rhs</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">func</span> * (lhs:<span style="color: #3495af" class="">Double</span>, rhs:<span style="color: #3495af" class="">Percentage</span>)-><span style="color: #3495af" class="">Double</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs * rhs.<span style="color: #3495af" class="">doubleValue</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">static</span><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">func</span><span style="color: #000000" class=""> * (lhs:</span>Percentage<span style="color: #000000" class="">, rhs:</span>CGFloat<span style="color: #000000" class="">)-></span>CGFloat<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs.<span style="color: #3495af" class="">cgfloatValue</span> * rhs</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">static</span><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">func</span><span style="color: #000000" class=""> * (lhs:</span>CGFloat<span style="color: #000000" class="">, rhs:</span>Percentage<span style="color: #000000" class="">)-></span>CGFloat<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs * rhs.<span style="color: #3495af" class="">cgfloatValue</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">func</span> * (lhs:<span style="color: #3495af" class="">Percentage</span>, rhs:<span style="color: #3495af" class="">Num</span>)-><span style="color: #3495af" class="">Num</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs.<span style="color: #3495af" class="">numValue</span> * rhs</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">func</span> * (lhs:<span style="color: #3495af" class="">Num</span>, rhs:<span style="color: #3495af" class="">Percentage</span>)-><span style="color: #3495af" class="">Num</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">return</span> lhs * rhs.<span style="color: #3495af" class="">numValue</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">static</span><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">func</span><span style="color: #000000" class=""> * (lhs:</span>Percentage<span style="color: #000000" class="">, rhs:</span>Percentage<span style="color: #000000" class="">)-></span>Percentage<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #0433ff" class="">return</span><span style="color: #000000" class=""> </span>Percentage<span style="color: #000000" class="">(lhs.</span>decimalValue<span style="color: #000000" class=""> * rhs.</span>decimalValue<span style="color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""> <br class=""><div><blockquote type="cite" class=""><div class="">On Jan 13, 2018, at 6:26 PM, Jonathan Hull via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi Evolution,<br class=""><br class="">I was wondering if we would consider adding a percentage type to Swift. This would be a type with a value between 0 & 1.<br class=""><br class="">I know we can and do use doubles or floats for this now, but there really is a semantic difference between most parameters that take a value between 0 & 1 and those that take any floating point value. It would be nice to have a type that semantically means that the value is from 0 to 1.<br class=""><br class="">It could even just wrap a Double for speed (in my own code I wrap a UInt64 for decimal accuracy… and to avoid issues around comparisons).<br class=""><br class="">Thanks,<br class="">Jon<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></body></html>