FeReTu FeReTu - 1 year ago 55
Swift Question

Int value with a dot minus an Int - swift

I want to hear if it is possible to minus two numbers, when the one value contains a (.) at the end of the number.

I have a value like this:

var numberOne: String = "21."

print(Int(numberOne) - 1)

That is not possible, it does not work. So how can I do that?

Answer Source

No that is not possible because the conversion to Int(numberOne) will fail because of the ..

Double/Float will work:

var numberOne: String = "21."
print(Double(numberOne)! - 1) // 20.0
print(Float(numberOne)! - 1) // 20.0