Answers for "decimal swift round"

5

round up swift

let number = 0.1
// Use ceil to remove the fractional part and round up.
let result = ceil(number)


let number1 = 1.1
// Use floor to remove the fractional part and round down.
let floor1 = floor(number1)
Posted by: Guest on June-19-2020
1

swift round double to 2 decimal places

You can use Swift's round function to accomplish this.

To round a Double with 3 digits precision, first multiply it by 1000, round it and divide the rounded result by 1000:

let x = 1.23556789
let y = Double(round(1000*x)/1000)
print(y)  // 1.236
Other than any kind of printf(...) or String(format: ...) solutions, the result of this operation is still of type Double.

EDIT:
Regarding the comments that it sometimes does not work, please read this:
Posted by: Guest on March-08-2021

Code answers related to "Swift"

Browse Popular Code Answers by Language