ylliX - Online Advertising Network Quick tip: Convert float to int in Swift - Yet Another Programmer's Blog

Quick tip: Convert float to int in Swift

It is a new language, ok. But why people can complex simple things? For years we used:

int newvar = (int)oldfloat;

and it was ok. Not only in ObjectiveC but in many other. Now you have to:

var newintvar:Int = Int(oldfloatvar)

BUT – keep in mind that float will be always ROUNDED DOWN – so for 1.9 you will get 1 not 2. To make it rounded as it should be use:

var newintvar:Int = Int(round(oldfloatvar))

 

Leave a Reply