How to convert the float to Int in Swift

How to convert the float to Int in Swift

In this post we will give you information about How to convert the float to Int in Swift. Hear we will give you detail about How to convert the float to Int in SwiftAnd how to use it also give you demo for it if it is necessary.

we will learn about how to convert the float to Int in Swift.

To convert a float value to an Int, we can use the Int() constructor by passing a float value to it.

Here is an example:

let myFloat: Float = 12.752let myInteger = Int(myFloat)print(myInteger)

Output:

12

Note: When we use this conversion the Integer is always rounded to the nearest downward value, like 12.752 to 12 or 6.99 to 6.

In case, if you want to round up the integer value then you can use the rounded() method like this.

let myFloat: Float = 3.4let myInteger = Int(myFloat.rounded(.up))print(myInteger)

Output:

4

Other example:

let myFloat: Float = 6.9let myInteger = Int(myFloat.rounded(.up)) // 7

Hope this code and post will helped you for implement How to convert the float to Int in Swift. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US