.rounded()
// ⭐️ .toNearestOrAwayFromZero (就是四捨五入)
(5.2).rounded() // 5.0
(5.5).rounded() // 6.0
(-5.2).rounded() // -5.0
(-5.5).rounded() // -6.0
let x = 6.5
// use rounding rules
x.rounded(.toNearestOrAwayFromZero) // round: 7.0
x.rounded(.towardZero)) // trunc: 6.0 ⭐️
x.rounded(.up) // ceil : 7.0 ⭐️
x.rounded(.down) // floor: 6.0 ⭐️
// ⭐️ Int
Int(3.25) // 3
Int(3.7) // 3
Int(-3.3) // -3
Int(-3.7) // -3Swift ⟩
Double ⟩
.rounded() - use .toNearestOrAwayFromZero rounding rule.
.rounded(_:) - use other rounding rules.
.round(), .round(_:) - round in place.
Int ⟩
init(_:)
Last updated
Was this helpful?