Here is my code
let ns = NumberFormatter.init()
ns.allowsFloats = true
ns.maximumFractionDigits = 18 //This is a variable value
ns.minimumFractionDigits = 18 //This is a variable value
ns.roundingMode = .floor
ns.numberStyle = .decimal
let doubleValueOfDecimal : Double = 12.95699999999998
let numb = NSNumber.init(value: doubleValueOfDecimal)
print(numb)
let string = ns.string(from: numb)
print(string)
The following is the output and input
doubleValueOfDecimal = 2.95699999999998
Output
2.95699999999998
Optional("2.956999999999980000")
But if I input
doubleValueOfDecimal = 12.95699999999998
The output is
12.95699999999998
Optional("12.957000000000000000")
The string conversion rounds up the value. Can someone explain me how this works?
The string conversion is rounding up the decimal places when I want it to show the exact number.