Joe Scotto
Joe Scotto

Reputation: 10877

Remove after certain number in Double

I'm using a loop to generate some Doubles to append to an array, the code itself is fine and working but the actual numbers have precision far more than I need.

For example, if a number it generates is 2.3632731 I only need the 2.3 part with no rounding. Is there a way using Swift to simply cut off after that one decimal place?

Upvotes: 1

Views: 311

Answers (1)

Damian Carrillo
Damian Carrillo

Reputation: 1218

You can just cast to an Int to truncate the decimal part and then back.

let x = 2.363271
let y = Double(Int(x * 10)) / 10.0

Upvotes: 1

Related Questions