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