實例方法
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 Float16 round(_:)用法及代碼示例
- Swift Float16 radix用法及代碼示例
- Swift Float16 remainder(dividingBy:)用法及代碼示例
- Swift Float16 random(in:)用法及代碼示例
- Swift Float16 random(in:using:)用法及代碼示例
- Swift Float16 ...(_:_:)用法及代碼示例
- Swift Float16 init(sign:exponent:significand:)用法及代碼示例
- Swift Float16 binade用法及代碼示例
- Swift Float16 /(_:_:)用法及代碼示例
- Swift Float16 -(_:_:)用法及代碼示例
- Swift Float16 magnitude用法及代碼示例
- Swift Float16 isZero用法及代碼示例
- Swift Float16 squareRoot()用法及代碼示例
- Swift Float16 truncatingRemainder(dividingBy:)用法及代碼示例
- Swift Float16 isTotallyOrdered(belowOrEqualTo:)用法及代碼示例
- Swift Float16 negate()用法及代碼示例
- Swift Float16 init(integerLiteral:)用法及代碼示例
- Swift Float16 *(_:_:)用法及代碼示例
- Swift Float16 isNaN用法及代碼示例
- Swift Float16 ..<(_:)用法及代碼示例
- Swift Float16 formTruncatingRemainder(dividingBy:)用法及代碼示例
- Swift Float16 -(_:)用法及代碼示例
- Swift Float16 init(exactly:)用法及代碼示例
- Swift Float16 +(_:)用法及代碼示例
- Swift Float16 formRemainder(dividingBy:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Float16 rounded(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。