Swift

Format Decimal Places

String Formatting

String(format: "%.1f", value)
Example
let pi = 3.14159

print(String(format: "%.0f", pi))
print(String(format: "%.1f", pi))
print(String(format: "%.2f", pi))
print(String(format: "%.3f", pi))
print(String(format: "%.4f", pi))
Output
3
3.1
3.14
3.142
3.1416

Using 'round' Function

Example
let pi = 3.14159

print(round(pi))
print(Double(round(10 * pi) / 10))
print(Double(round(100 * pi) / 100))
print(Double(round(1000 * pi) / 1000))
print(Double(round(10000 * pi) / 10000))
Output
3.0
3.1
3.14
3.142
3.1416

-Swift