Rounding a double value to x number of decimal places in swift
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
Unlike 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: What Every Programmer Should Know About Floating-Point Arithmetic
Round up double to 2 decimal places
Use a format string to round up to two decimal places and convert the double
to a String
:
let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)!
railRatioLabelField.text! = String(format: "%.2f", currentRatio)
Example:
let myDouble = 3.141
let doubleStr = String(format: "%.2f", myDouble) // "3.14"
If you want to round up your last decimal place, you could do something like this (thanks Phoen1xUK):
let myDouble = 3.141
let doubleStr = String(format: "%.2f", ceil(myDouble*100)/100) // "3.15"
Rounding a double to 2 decimal places in Swift, XCode 12
You could do it directly inside Text
with the help of string interpolation:
struct ContentView: View {
let decimalNumber = 12.939010
var body: some View {
Text("\(decimalNumber, specifier: "%.2f")")//displays 12.94
}
}
Round a digit upto two decimal place in Swift
Use a format string to round up to two decimal places and convert the double to a String:
let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)!
railRatioLabelField.text! = String(format: "%.2f", currentRatio)
Example:
let myDouble = 3.141
let doubleStr = Double(String(format: "%.2f", myDouble)) // 3.14
let myDouble = 3.141
let doubleStr = String(format: "%.2f", myDouble) // "3.14"
If you want to round up your last decimal place, you could do something like this :
let myDouble = 3.141
let doubleStr = Double(String(format: "%.2f", ceil(myDouble*100)/100)) // 3.15
let myDouble = 3.141
let doubleStr = String(format: "%.2f", ceil(myDouble*100)/100) // "3.15"
How to round a double to the third digit after decimal point
You can do this by
extension Formatter {
static let number = NumberFormatter()
}
extension FloatingPoint {
var asNumberString : String {
Formatter.number.minimumFractionDigits = 2
Formatter.number.maximumFractionDigits = 2
Formatter.number.roundingMode = .halfEven
Formatter.number.numberStyle = .decimal
return Formatter.number.string(for: self) ?? ""
}
}
Now You can test like
150.51581.asNumberString === > "150.52"
150.5141531.asNumberString === > "150.51"
How to get a Double value up to 2 Decimal places
You can simply do this:
let pi = 3.14159
let text = String(format: "%.2f", arguments: [pi])
print(text) // output = 3.14
Related Topics
Prevent Dismissal of Uialertcontroller
Using a Type Variable in a Generic
Instance Member Cannot Be Used on Type
How to Create Local Scopes in Swift
Call a Method from a String in Swift
How to Convert a View (Not Uiview) to an Image
Swiftui Iterating Through Dictionary With Foreach
Get Integer Value from String in Swift
Swift 2 - Pattern Matching in "If"
Get Terminal Output After a Command Swift
Closure With Generic Parameters
What Does a Module Mean in Swift
Arkit - What Do the Different Columns in Transform Matrix Represent
Decrement Index in a Loop After Swift C-Style Loops Deprecated