Required Instance Methodswift 6.0.1Swift
round(_:)
Rounds the value to an integral value using the specified rounding rule.
mutating func round(_ rule: FloatingPointRoundingRule)
Parameters
- rule
The rounding rule to use.
The following example rounds a value using four different rounding rules:
// 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
For more information about the available rounding rules, see the FloatingPointRoundingRule
enumeration. To round a value using the default “schoolbook rounding”, you can use the shorter round()
method instead.
var w1 = 6.5
w1.round()
// w1 == 7.0
Other requirements
Type members
associatedtype Exponent
A type that can represent any written exponent.
init(Int
) Creates a new value, rounded to the closest possible representation.
init<Source>(Source
) Creates a new value, rounded to the closest possible representation.
init?<Source>(exactly: Source
) Creates a new value, if the given integer can be represented exactly.
init(sign: FloatingPointSign, exponent: Self.Exponent, significand: Self
) Creates a new value from the given sign, exponent, and significand.
init(signOf: Self, magnitudeOf: Self
) Creates a new floating-point value using the sign of one value and the magnitude of another.
static var greatestFiniteMagnitude: Self
The greatest finite number representable by this type.
static var infinity: Self
Positive infinity.
static var leastNonzeroMagnitude: Self
The least positive number.
static var leastNormalMagnitude: Self
The least positive normal number.
static var nan: Self
A quiet NaN (“not a number”).
static var pi: Self
The mathematical constant pi (π), approximately equal to 3.14159.
static var radix: Int
The radix, or base of exponentiation, for a floating-point type.
static var signalingNaN: Self
A signaling NaN (“not a number”).
static var ulpOfOne: Self
The unit in the last place of 1.0.
static func maximum(Self, Self
) -> Self Returns the greater of the two given values.
static func maximumMagnitude(Self, Self
) -> Self Returns the value with greater magnitude.
static func minimum(Self, Self
) -> Self Returns the lesser of the two given values.
static func minimumMagnitude(Self, Self
) -> Self Returns the value with lesser magnitude.
static func * (lhs: Self, rhs: Self
) -> Self Multiplies two values and produces their product, rounding to a representable value.
static func *= (lhs: inout Self, rhs: Self
) Multiplies two values and stores the result in the left-hand-side variable, rounding to a representable value.
static func + (lhs: Self, rhs: Self
) -> Self Adds two values and produces their sum, rounded to a representable value.
static func += (lhs: inout Self, rhs: Self
) Adds two values and stores the result in the left-hand-side variable, rounded to a representable value.
static func - (operand: Self
) -> Self Calculates the additive inverse of a value.
static func - (lhs: Self, rhs: Self
) -> Self Subtracts one value from another and produces their difference, rounded to a representable value.
static func -= (lhs: inout Self, rhs: Self
) Subtracts the second value from the first and stores the difference in the left-hand-side variable, rounding to a representable value.
static func / (lhs: Self, rhs: Self
) -> Self Returns the quotient of dividing the first value by the second, rounded to a representable value.
static func /= (lhs: inout Self, rhs: Self
) Divides the first value by the second and stores the quotient in the left-hand-side variable, rounding to a representable value.
Instance members
var exponent: Self.Exponent
The exponent of the floating-point value.
var floatingPointClass: FloatingPointClassification
The classification of this value.
var isCanonical: Bool
A Boolean value indicating whether the instance’s representation is in its canonical form.
var isFinite: Bool
A Boolean value indicating whether this instance is finite.
var isInfinite: Bool
A Boolean value indicating whether the instance is infinite.
var isNaN: Bool
A Boolean value indicating whether the instance is NaN (“not a number”).
var isNormal: Bool
A Boolean value indicating whether this instance is normal.
var isSignalingNaN: Bool
A Boolean value indicating whether the instance is a signaling NaN.
var isSubnormal: Bool
A Boolean value indicating whether the instance is subnormal.
var isZero: Bool
A Boolean value indicating whether the instance is equal to zero.
var nextDown: Self
The greatest representable value that compares less than this value.
var nextUp: Self
The least representable value that compares greater than this value.
var sign: FloatingPointSign
The sign of the floating-point value.
var significand: Self
The significand of the floating-point value.
var ulp: Self
The unit in the last place of this value.
func addProduct(Self, Self
) Adds the product of the two given values to this value in place, computed without intermediate rounding.
func addingProduct(Self, Self
) -> Self Returns the result of adding the product of the two given values to this value, computed without intermediate rounding.
func formRemainder(dividingBy: Self
) Replaces this value with the remainder of itself divided by the given value.
func formSquareRoot(
) Replaces this value with its square root, rounded to a representable value.
func formTruncatingRemainder(dividingBy: Self
) Replaces this value with the remainder of itself divided by the given value using truncating division.
func isEqual(to: Self
) -> Bool Returns a Boolean value indicating whether this instance is equal to the given value.
func isLess(than: Self
) -> Bool Returns a Boolean value indicating whether this instance is less than the given value.
func isLessThanOrEqualTo(Self
) -> Bool Returns a Boolean value indicating whether this instance is less than or equal to the given value.
func isTotallyOrdered(belowOrEqualTo: Self
) -> Bool Returns a Boolean value indicating whether this instance should precede or tie positions with the given value in an ascending sort.
func negate(
) Replaces this value with its additive inverse.
func remainder(dividingBy: Self
) -> Self Returns the remainder of this value divided by the given value.
func rounded(FloatingPointRoundingRule
) -> Self Returns this value rounded to an integral value using the specified rounding rule.
func squareRoot(
) -> Self Returns the square root of the value, rounded to a representable value.
func truncatingRemainder(dividingBy: Self
) -> Self Returns the remainder of this value divided by the given value using truncating division.