Convert Float Value to String in Swift

Convert float value to String in Swift

If you want some more control of how it's converted you can either use +stringWithFormat on NSString or NSNumberFormatter

let f = -33.861382
let s = NSString(format: "%.2f", f)

let nf = NSNumberFormatter()
nf.numberStyle = .DecimalStyle
// Configure the number formatter to your liking
let s2 = nf.stringFromNumber(f)

Convert a float into a string in Swift iOS

Similar to Connor's answer, you can do the following to have a little more control about how your Double or Float is dislpayed...

let myStringToTwoDecimals = String(format:"%.2f", myFloat)

This is basically like stringWithFormat in objC.

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)
}

In Swift 5, How to convert a Float to a String localized in order to display it in a textField?

Here is finaly a solution:

extension Float {

func afficherUnFloat() -> String {
let text : NSNumber = self as NSNumber
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = .current
numberFormatter.groupingSeparator = ""
numberFormatter.maximumFractionDigits = 2 // your choice
numberFormatter.maximumIntegerDigits = 6 // your choice

let result = numberFormatter.string(from: text) ?? ""
return result
}

}

With this, you can format every Float to a localized String, compatible with the keyboard choosen by the user, regardless of his locality or langage.
There is no need to force a special keyboard to have a specific decimal separator.

you can use it like this:

let myFloat: Float = 111.222
let myString :String = myFloat.afficherUnFloat()

myString will be displayed as the location requires

ios swift proper float to string conversion

You'd do it like this:

scoreNameLabel.text = "\(convertUserScoreToString)"

String Interpolation

Swift extract an Int, Float or Double value from a String (type-conversion)

Convert String to NSString and Use convenience methods:

var str = "3.1"

To Int

var intValue : Int = NSString(string: str).integerValue // 3

To Float

var floatValue : Float = NSString(string: str).floatValue // 3.09999990463257

To Double

var doubleValue : Double = NSString(string: str).doubleValue // 3.1


Reference

var doubleValue: Double { get }
var floatValue: Float { get }
var intValue: Int32 { get }
@availability(OSX, introduced=10.5)
var integerValue: Int { get }
@availability(OSX, introduced=10.5)
var longLongValue: Int64 { get }
@availability(OSX, introduced=10.5)

Swift: converting String to Float and back to String again after doing some mathematical operations

Actually floats can not represent numbers accurately, you'll have to use Double.
Here is a very nice answer on that issue:
https://stackoverflow.com/a/3730040/4662531

EDIT:

Sorry, but actually Double should not be use to perform calculations (I'm assuming from the naming of your variables you are working on some banking things). That part of the above linked answer is really giving a great suggestion:

A solution that works in just about any language is to use integers
instead, and count cents. For instance, 1025 would be $10.25. Several
languages also have built-in types to deal with money. Among others,
Java has the BigDecimal class, and C# has the decimal type.

A colleague of mine that used to work in a banking company also confirmed that all calculations were done without using Floats or Double, but with Int as suggested in the link.

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

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)


Related Topics



Leave a reply



Submit