Conversion Between Swift numeric types, Swift numeric type conversion

Source: Internet
Author: User

Conversion Between Swift numeric types, Swift numeric type conversion
Conversion Between Swift numeric types Swift is a secure language that requires strict type checks and cannot be converted between different types.
1. Integer Conversion
In other languages such as C and Objective-C, there are two conversion methods between integers:
It is automatically converted from small to large;
Forced type conversion is required to count from a large range to a small range, which may cause loss of data precision.
In Swift, the two methods do not work. We need to use some functions for explicit conversion. The Code is as follows:

Let historyScore: UInt8 = 90let englishScore: UInt16 = 130let totalScore = historyScore + englishScore // error ① let totalScore = UInt16 (historyScore) + englishScore // correct ② let totalScore = historyScore + UInt8 (englishScore) // correct ③


The above Code declares and initializes two constants, historyScore and engishscore. We add them and assign them to totalScore. If you add the Code in line ①, the program will have a compilation error because historyScore is of the UInt8 type, while engishscore is of the UInt16 type, which cannot be converted between them.
We have two conversion methods.
One is to convert the historyScore of UInt8 to the UInt16 type. Since it is converted from a small number to a large number, this conversion is safe. Line ② UInt16 (historyScore) of the Code is the correct conversion method.
The other is to convert the engishscore of UInt16 to the UInt8 type. Because the number is converted from a large range to a small range, this conversion is not safe. If the number of values to be converted is compared, the accuracy will be lost. Line ③ of the Code UInt8 (englishScore) is the correct conversion method. In this example, the value of englishScore is 130, and the conversion is successful. If you change this number to 1300, although the program compilation is normal, the exception information will be output in the console, this is an exception during runtime.
In the above Code, UInt16 (historyScore) and UInt8 (engishscore) are actually constructors that can create and initialize another type. For more information about the constructor, see Chapter 14th.
2. Conversion between integer and floating point types
The conversion between integer and floating point types is similar to that between integer types. Therefore, we modify the example in the previous section as follows:
Let historyScore: Float = 90.6 ① let englishScore: UInt16 = 130 ② let totalScore = historyScore + englishScore // error ③ let totalScore = historyScore + Float (englishScore) // correct, security ④ let totalScore = UInt16 (historyScore) + englishScore // correct. The decimal number is truncated. ⑤


The above code has been modified. The historyScore variable type of the Code in line ① Is Float. The variable in line ② code is of the UInt16 type. The Code in line ③ is calculated directly, and the result is a compilation error. Line 4 of the Code converts the engishscore variable of the UInt16 type to the Float type, which is the safest. The Code in line ⑤ converts the Float type historyScore variable to the UInt16 type. This conversion will first cause the fractional part to be truncated. In addition, if the historyScore variable is large, it will cause an exception during runtime, this is similar to the integer type conversion.


For more information, please refer to the first domestic Swift book "Swift development guide" for discussion. Website: http://www.51work6.com/swift.phpwelcome to the swifttechnology discussion group: 362298.pdf

Welcome to Zhijie iOS public classroom Platform



In C #, is there a way to convert time to the pure numeric type, for example, 2012-3-21 15:02:23 to: 0212321150223?

DateTime dt = DateTime. Now;
String ds = dt. ToString ("yyyyMMddHHmmss ");
Double dd = Convert. ToDouble (ds );

Here, ds is in string format, and dd is in number format with 20120321190951 content

Conversion between characters and numbers in VB

The digital data in the Access database includes integer, long integer, single precision, double precision, and bytes. "The result decimal point disappears." Your data type may be integer, long integer, or byte data.
The formats of Single-precision data displayed in vb are 32.5, 3.2, and ,. 8. For example, 30.2 and 03.5 are displayed as 30.2 and 3.5. If you must display them as 30.2 and 03.5, you can convert them into strings.
Functions for converting numbers to strings: CStr (), Str ()
Function for converting a string to a number: val ()
For example, the number to display 3.5 Is 03.5: A = "0" & CStr (3.5)

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.