Instance Method インスタンスメソッド

round(_:)

Rounds the value to an integral value using the specified rounding rule. 指定された丸め規則を使ってこの値を整数値に丸めます。

Declaration 宣言

mutating func round(_ rule: FloatingPointRoundingRule)

Parameters パラメータ

rule

The rounding rule to use. この丸め規則を使います。

Discussion 解説

The following example rounds a value using four different rounding rules: 以下の例はある値を4つの丸め規則を使って丸めます:


// 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. 利用可能な丸め規則についてのさらなる情報として、FloatingPointRoundingRule列挙を見てください。省略時の「教科書丸め」を使って値を丸めるには、あなたはより短いround()メソッドを代わりに使うことができます。


var w1 = 6.5
w1.round()
// w1 == 7.0

Relationships 関係

From Protocol 由来プロトコル

See Also 参照

Rounding Values 値を丸める