Search code examples
swiftdouble

How to divide a double value without rounding in swift?


I have to divide a big double number by another in my application which will show 8 digit after floating point. Here is the swift code:

let firstValue:Double = 355531194300085860
let secondValue:Double = 100000000
let result = String(format: "%.8f", firstValue/secondValue)
print("value: \(result)")  

I am expecting the output as 3555311943.00085860 but compiler giving output like this 3555311943.00085878 . Why this is happening and how can i fix it?


Solution

  • Try this

    let firstValue = NSDecimalNumber(string: "355531194300085860")
    let secondValue = NSDecimalNumber(string: "100000000")
    let result = firstValue.dividing(by: secondValue)
    print(result)
    

    Result: 3555311943.0008586