Since a float is bigger than int, you can convert a float to an int by simply down-casting it e.g. (int) 4.0f will give you integer 4. By the way, you must remember that typecasting just get rid of anything after the decimal point, they don't perform any rounding or flooring operation on the value.
text = String(format:"%. 2f", (Float)(self. creditCardTXTField. text!)
swift1min read To convert a float value to an Int, we can use the Int() constructor by passing a float value to it. 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 .
As a result of all this, Swift will refuse to automatically convert between its various numeric types – you can't add an Int and a Double , you can't multiply a Float and an Int , and so on.
You can convert Float
to Int
in Swift like this:
var myIntValue:Int = Int(myFloatValue)
println "My value is \(myIntValue)"
You can also achieve this result with @paulm's comment:
var myIntValue = Int(myFloatValue)
Converting to Int will lose any precision (effectively rounding down). By accessing the math libraries you can perform explicit conversions. For example:
If you wanted to round down and convert to integer:
let f = 10.51
let y = Int(floor(f))
result is 10.
If you wanted to round up and convert to integer:
let f = 10.51
let y = Int(ceil(f))
result is 11.
If you want to explicitly round to the nearest integer
let f = 10.51
let y = Int(round(f))
result is 11.
In the latter case, this might seem pedantic, but it's semantically clearer as there is no implicit conversion...important if you're doing signal processing for example.
There are lots of ways to round number with precision. You should eventually use swift's standard library method rounded()
to round float number with desired precision.
To round up use .up
rule:
let f: Float = 2.2
let i = Int(f.rounded(.up)) // 3
To round down use .down
rule:
let f: Float = 2.2
let i = Int(f.rounded(.down)) // 2
To round to the nearest integer use .toNearestOrEven
rule:
let f: Float = 2.2
let i = Int(f.rounded(.toNearestOrEven)) // 2
Be aware of the following example:
let f: Float = 2.5
let i = Int(roundf(f)) // 3
let j = Int(f.rounded(.toNearestOrEven)) // 2
Converting is simple:
let float = Float(1.1) // 1.1
let int = Int(float) // 1
But it is not safe:
let float = Float(Int.max) + 1
let int = Int(float)
Will due to a nice crash:
fatal error: floating point value can not be converted to Int because it is greater than Int.max
So I've created an extension that handles overflow:
extension Double {
// If you don't want your code crash on each overflow, use this function that operates on optionals
// E.g.: Int(Double(Int.max) + 1) will crash:
// fatal error: floating point value can not be converted to Int because it is greater than Int.max
func toInt() -> Int? {
if self > Double(Int.min) && self < Double(Int.max) {
return Int(self)
} else {
return nil
}
}
}
extension Float {
func toInt() -> Int? {
if self > Float(Int.min) && self < Float(Int.max) {
return Int(self)
} else {
return nil
}
}
}
I hope this can help someone
You can get an integer representation of your float by passing the float into the Integer initializer method.
Example:
Int(myFloat)
Keep in mind, that any numbers after the decimal point will be loss. Meaning, 3.9 is an Int of 3 and 8.99999 is an integer of 8.
Like this:
var float:Float = 2.2 // 2.2
var integer:Int = Int(float) // 2 .. will always round down. 3.9 will be 3
var anotherFloat: Float = Float(integer) // 2.0
Use a function style conversion (found in section labeled "Integer and Floating-Point Conversion" from "The Swift Programming Language."[iTunes link])
1> Int(3.4)
$R1: Int = 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With