Instance Method

rounded()

Returns this value rounded to an integral value using “schoolbook rounding.”

Declaration

func rounded() -> Double

Return Value

The nearest integral value, or, if two integral values are equally close, the integral value with greater magnitude.

Discussion

The rounded() method uses the .toNearestOrAwayFromZero rounding rule, where a value halfway between two integral values is rounded to the one with greater magnitude. The following example rounds several values using this default rule:

(5.2).rounded()
// 5.0
(5.5).rounded()
// 6.0
(-5.2).rounded()
// -5.0
(-5.5).rounded()
// -6.0

To specify an alternative rule for rounding, use the rounded(_:) method instead.

See Also

Rounding Values

func rounded(FloatingPointRoundingRule) -> Double

Returns this value rounded to an integral value using the specified rounding rule.

func round()

Rounds this value to an integral value using “schoolbook rounding.”

func round(FloatingPointRoundingRule)

Rounds the value to an integral value using the specified rounding rule.