Convert Float to Int in Swift

Convert Float to Int in Swift

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)

Safe conversion of Float to Int?

Int(exactly:) might be what you are looking for:

Creates an integer from the given floating-point value, if it can be represented exactly.

If the value passed as source is not representable exactly, the result is nil.

Example:

let x = 123e20
if let i = Int(exactly: x) {
print(i)
} else {
print("not representable")
}

This will also fail if the floating point number is not integral, so you might want to round it before the conversion:

let x = 12.3
if let i = Int(exactly: x.rounded(.towardZero)) {
print(i)
}

Rounding towards zero is what Int(x) would do, you can pick your desired rounding mode.

Convert string (float value) to int in Swift

Option 1

let intNum = Int(Float(floatstring)!)

Option 2

if floatstring.rangeOfString(".") != nil {
let i = Int(floatstring.componentsSeparatedByString(".").first!)
print(i)
}

How do I convert Float to Int When Necessary?

    let number1 = 1.0
let number2 = 1.2

let str = String(format: number1 == floor(number1) ? "%.0f":"%.1f", number1)
print(str)
//prints 1

let str2 = String(format: number2 == floor(number2) ? "%.0f":"%.1f", number2)
print(str2)
//prints 1.2

How can I convert an integer to float value in textfield in swift ios?

You can display textfield value as a float like appending ".00" string with textField like,

func textFieldDidEndEditing(textField: UITextField)
{
if(textField == self.phoneNUmberTxtField)
{
self.creditCardTXTField.text = String(format:"%.2f", (Float)(self.creditCardTXTField.text!)!)
}

}

NOTE: this thing also possible with textFieldShouldReturn method.

Hope this will help you

Accept Float, Double or Int in Swift `init` to convert to String

Actually if all you want is a string representation of Int Float Double or any other standard numeric type you only need to know that they conform to CustomStringConvertible and use String(describing:).

Or you can use conformance to Numeric and CustomStringConvertible:

struct example {
var string: String

init<C: CustomStringConvertible & Numeric>(number: C) {
string = String(describing: number)
}
}

and maybe even better example itself could conform to CustomStringConvertible

struct example: CustomStringConvertible {
var description: String

init<C: CustomStringConvertible & Numeric>(number: C) {
description = String(describing: number)
}
}

yet another way :

struct example<N: Numeric & CustomStringConvertible>: CustomStringConvertible {
let number: N
init(number: N) {
self.number = number
}
var description: String {
String(describing: number)
}
}

EDIT

I think what you want is a custom Property Wrapper not @Binding:

@propertyWrapper struct CustomStringConversion<Wrapped: CustomStringConvertible> {
var wrappedValue: Wrapped

init(wrappedValue: Wrapped) {
self.wrappedValue = wrappedValue
}

var projectedValue: String { .init(describing: wrappedValue) }
}

struct Foo {
@CustomStringConversion var number = 5
}

let foo = Foo()
let number: Int = foo.number // 5
let stringRepresentation: String = foo.$number // "5"

But as @LeoDabus pointed out using LosslessStringConvertible may be better :

struct example<N: Numeric & LosslessStringConvertible>: LosslessStringConvertible {
let number: N

init(number: N) {
self.number = number
}

init?(_ description: String) {
guard let number = N(description) else { return nil }
self.number = number
}

var description: String {
.init(number)
}
}

let bar = example(number: Double.greatestFiniteMagnitude) // 1.7976931348623157e+308
let baz: example<Double>? = example("1.7976931348623157e+308") // 1.7976931348623157e+308


Related Topics



Leave a reply



Submit