Introduction to Swift (ix)--string and int, Double, float and other numbers convert each other

Source: Internet
Author: User
Tags string to number

Three modes of conversion

In any language, numeric types, such as Int, float, double, are self-contained, but the conversion between them and string types is always inconvenient, and here is a summary of how they convert each other. Summed up a total of three conversion modes, respectively, illustrate.

One, string to number

Here is an example of a string type to int type. String to other numeric types (Float, double, and so on) are similar. The main method used is the ToInt method of the string type. Note that this method returns an int? , which is an integer optional type. So we need to unpack.

varstring"1234"var0ifletstring.toInt(){    n = number}

This gives the int type variable n with a value of 1234. In fact, at first I thought that if n has no value (that is, it has not been assigned a value of 0), the solution fails, then what is the result of println (n), the default is 0?

However, after careful consideration, it is impossible to see this situation happen. Swift does not allow us to use a variable that has not yet been initialized. Otherwise the compiler will error:

Variable ' n ' used before being initialized

A bit far away, of course, can also be used directly

varstring.toInt()!

But it is strongly not recommended. Even if you are pretty sure that the string must be converted to int.

Second, digital to string

Here is an example of converting string with a double type. Compare a simple notation as follows:

var123.5varstring"\(number)"

This gives a string of type variable value "123.5". However, if you want to write a method that allows number to be passed in as a parameter, this shorthand will not be possible. We can take advantage of the string type initialization method.

var123.5varstringString(stringInterpolationSegment: number)

The double type is written because, if number is an int type, calling string (number) directly generates a variable of type string, and now the number is a double type. So you need to explicitly add the external variable name stringinterpolationsegment. This notation is still true for the int type, but it's a little cumbersome, but without the external variable name, you can't convert the double to a string.

The second type of writing is recommended by individuals.

Iii. reciprocal Transfer of numbers

After you understand how numbers and strings are converted, take a look at the simplest types of numbers for each other. Here is an example of double to int.

Like C, Java, OC, and other languages, you can use the coercion type conversion method:

var doubleNumber = 1.8var intNumber = 0intNumber = Int(doubleNumber)println(intNumber)

The output here is 1. That is, the doublenumber is not rounded, but the accuracy is lost.

Or use a rigorous construction method to write:

var doubleNumber = 1.8var intNumber = 0intNumber = Int(doubleNumber)println(intNumber)

The output is still 1.

Appendix to the full column-"Swift easy access"

"Introduction to Swift (i)--basic syntax"
"Introduction to Swift (ii)--character and string"
"Introduction to Swift (iii)--tuples (tuple)"
"Introduction to Swift (iv)--optional type (optionals) and assert (Assert)"
"Getting Started with Swift (v)--Arrays (array)"
"Introduction to Swift (vi)--Dictionary (Dictionary)"
"Introduction to Swift (vii)-structure (struct)"
"Introduction to Swift (eight)--powerful redundancy operator"
"Introduction to Swift (ix)--string and int, Double, float and other numbers convert each other"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction to Swift (ix)--string and int, Double, float and other numbers convert each other

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.