Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation in Swift

A function in swift takes any numeric type in Swift (Int, Double, Float, UInt, etc). the function converts the number to a string

the function signature is as follows :

func swiftNumbers <T : NumericType> (number : T) -> String {
    //body
}

NumericType is a custom protocol that has been added to numeric types in Swift.

inside the body of the function, the number should be converted to a string:

I use the following

var stringFromNumber = "\(number)"

which is not so elegant, PLUS : if the absolute value of the number is strictly inferior to 0.0001 it gives this:

"\(0.000099)" //"9.9e-05"

or if the number is a big number :

"\(999999999999999999.9999)" //"1e+18"

is there a way to work around this string interpolation limitation? (without using Objective-C)

P.S :

NumberFormater doesn't work either

import Foundation

let number : NSNumber = 9_999_999_999_999_997

let formatter = NumberFormatter()
formatter.minimumFractionDigits = 20
formatter.minimumIntegerDigits = 20
formatter.minimumSignificantDigits = 40

formatter.string(from: number) // "9999999999999996.000000000000000000000000"

let stringFromNumber = String(format: "%20.20f", number) // "0.00000000000000000000"
like image 347
ielyamani Avatar asked Aug 31 '14 19:08

ielyamani


People also ask

What is meant by string interpolation?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

How is string interpolation performed?

String interpolation is a process of injecting value into a placeholder (a placeholder is nothing but a variable to which you can assign data/value later) in a string literal. It helps in dynamically formatting the output in a fancier way. Python supports multiple ways to format string literals.

How do I combine strings in Swift?

In the apple documentation for swift it states that you concatenate 2 strings by using the additions operator e.g. : let string1 = "hello" let string2 = " there" var welcome = string1 + string2 4 // welcome now equals "hello there"

Why is string interpolation used?

String Interpolation is a process in which the placeholder characters are replaced with the variables (or strings in this case) which allow to dynamically or efficiently print out text output. String Interpolation makes the code more compact and avoids repetition of using variables to print the output.


2 Answers

Swift String Interpolation

1) Adding different types to a string

2) Means the string is created from a mix of constants, variables, literals or expressions.

Example:

let length:Float = 3.14
var breadth = 10
var myString = "Area of a rectangle is length*breadth"
myString = "\(myString) i.e. = \(length)*\(breadth)"    

Output:

3.14
10
Area of a rectangle is length*breadth
Area of a rectangle is length*breadth i.e. = 3.14*10
like image 186
Anitha Avatar answered Oct 02 '22 08:10

Anitha


Use the Swift String initializer: String(format: <#String#>, arguments: <#[CVarArgType]#>) For example: let stringFromNumber = String(format: "%.2f", number)

like image 23
chrisinsfo Avatar answered Oct 02 '22 08:10

chrisinsfo