實例方法
round(_:)
使用指定的舍入規則將值舍入為整數值。
必需的。
聲明
mutating func round(_ rule: FloatingPointRoundingRule)
參數
rule
要使用的舍入規則。
詳述
以下示例使用四種不同的舍入規則對值進行舍入:
// Equivalent to the C 'round' function:
var w = 6.5
w.round(.toNearestOrAwayFromZero)
// w == 7.0
// Equivalent to the C 'trunc' function:
var x = 6.5
x.round(.towardZero)
// x == 6.0
// Equivalent to the C 'ceil' function:
var y = 6.5
y.round(.up)
// y == 7.0
// Equivalent to the C 'floor' function:
var z = 6.5
z.round(.down)
// z == 6.0
有關可用舍入規則的詳細信息,請參閱FloatingPointRoundingRule
枚舉。要使用默認 “schoolbook rounding” 對值進行舍入,您可以改用較短的 round()
方法。
var w1 = 6.5
w1.round()
// w1 == 7.0
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift FloatingPoint rounded(_:)用法及代碼示例
- Swift FloatingPoint remainder(dividingBy:)用法及代碼示例
- Swift FloatingPoint radix用法及代碼示例
- Swift FloatingPoint pi用法及代碼示例
- Swift FloatingPoint truncatingRemainder(dividingBy:)用法及代碼示例
- Swift FloatingPoint infinity用法及代碼示例
- Swift FloatingPoint nan用法及代碼示例
- Swift FloatingPoint isEqual(to:)用法及代碼示例
- Swift FloatingPoint isZero用法及代碼示例
- Swift FloatingPoint isNaN用法及代碼示例
- Swift FloatingPoint -(_:_:)用法及代碼示例
- Swift FloatingPoint maximum(_:_:)用法及代碼示例
- Swift FloatingPoint exponent用法及代碼示例
- Swift FloatingPoint init(signOf:magnitudeOf:)用法及代碼示例
- Swift FloatingPoint /(_:_:)用法及代碼示例
- Swift FloatingPoint *(_:_:)用法及代碼示例
- Swift FloatingPoint init(sign:exponent:significand:)用法及代碼示例
- Swift FloatingPoint -(_:)用法及代碼示例
- Swift FloatingPoint significand用法及代碼示例
- Swift FloatingPoint minimum(_:_:)用法及代碼示例
- Swift FloatingPoint formTruncatingRemainder(dividingBy:)用法及代碼示例
- Swift FloatingPoint maximumMagnitude(_:_:)用法及代碼示例
- Swift FloatingPoint formRemainder(dividingBy:)用法及代碼示例
- Swift FloatingPoint negate()用法及代碼示例
- Swift FloatingPoint sign用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 FloatingPoint round(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。