Converting String to Int with Swift

前端 未结 30 1290
小鲜肉
小鲜肉 2020-11-22 06:59

The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the value

30条回答
  •  一生所求
    2020-11-22 07:03

    Question : string "4.0000" can not be convert into integer using Int("4.000")?

    Answer : Int() check string is integer or not if yes then give you integer and otherwise nil. but Float or Double can convert any number string to respective Float or Double without giving nil. Example if you have "45" integer string but using Float("45") gives you 45.0 float value or using Double("4567") gives you 45.0.

    Solution : NSString(string: "45.000").integerValue or Int(Float("45.000")!)! to get correct result.

提交回复
热议问题