Below is my code.. I am doing 5 2 and I would like the output to be 7 but I am getting 7.0. Can someone please help me and tell me why
var runningNumber:Double = 5
var currentValue:Double = 2
var total:Double = Double(runningNumber currentValue)
print(total)
CodePudding user response:
Consider using .formatted()
to format the floating point number properly for you:
import Foundation
⋮
⋮
⋮
print(total.formatted())
CodePudding user response:
you could just do this:
print(String(format:"%0.0f", total))
CodePudding user response:
- Use
Int
s instead ofDouble
s
let two = 2
let five = 5
print(two five)
- Cast to
Int
when printing
let two: Double = 2
let five: Double = 5
print(Int(two five))
- Use String formatter when printing
let two: Double = 2
let five: Double = 5
print(String(format: "%.0f", two five))
- Use
NumberFormatter
let two: Double = 2
let five: Double = 5
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0
let sum = NSNumber(value: two five)
print(formatter.string(from: sum)!)
CodePudding user response:
I got it working but changing the code to this
var runningNumber:Double = 5
var currentValue:Double = 2
var total:Double = NSNumber(value: runningNumber currentValue).stringValue
print(total)