Number literal
The literal expression of an integer is as follows:
· Decimal number, No prefix
· Binary number, Prefixed with 0 B
· October, With the 0o prefix
· Hexadecimal number, Prefixed with 0x
The decimal values of all the following integers are 17:
1 let decimalInteger = 172 let binaryInteger = 0b10001 // 17 binary ID 3 let octalInteger = 0o21 // 17 4 let hexadecimalInteger = 0x11 // 17 hexadecimal ID
The floating point literal can be binary (without a prefix) or hexadecimal (prefixed with 0x). Each decimal point must have a number (or hexadecimal number ). Both of them can have an optional exponential power. Decimal decimal digits are marked in upper or lower case.E, In hexadecimal formatP.
Exponential powerExpDecimal number, whose base number is multiplied by 10Exp:
·1.25e2 indicates 1.25 × 102, or 125.0.
·1.25e-2 indicates 1.25x10-2, or 0.0125.
Exponential powerExpThe hexadecimal number, whose base number is multiplied by 2Exp:
·0xFp2 indicates 15 × 22, or 60.0.
·0xFp-2 indicates 15 × 2-2, or 3.75.
The hexadecimal values of all floating point numbers below are 12.1875:
1 let decimalDouble = 12.18752 let exponentDouble = 1.21875e13 let hexadecimalDouble = 0xC.3p0
Numbers can contain other formats for ease of reading. Both integers and floating-point numbers can be added with zero or underline to improve readability. The two formats do not affect the actual value of the literal volume:
1 let paddedDouble = 000123.4562 let oneMillion = 1_000_0003 let justOverOneMillion = 1_000_000.000_000_1
Digital type conversion
Int should be used in the codeTypes are Integer constants and variable types for all common purposes, even if they are indeed non-negative. In daily use, the use of the default Integer type means that these Integer constants and variables can instantly participate in the operation, and can match the type inferred based on the integer nominal value.
They are used only when tasks in the hands must use other integer types. For example, external data sources provide data with clear width, or for performance, memory usage, and other necessary optimization considerations. In these cases, the use of clear width types helps to detect accidental numeric overflow and capture the original information of data in use.
Integer Conversion
Different numeric types can store different value ranges. OneInt8A constant or variable can store numbers ranging from-128 to 127, whileUInt8Constants or variables can store numbers ranging from 0 to 255. An error is reported during compilation when a constant or variable cannot be saved into an integer:
1 let cannotBeNegative: UInt8 =-12 // UInt8 cannot save negative numbers, so an error will be reported here
3 let tooBig: Int8 = Int8.max + 14 // Int8 cannot store numbers that exceed its maximum value range.
Because different data types can store different value ranges, you must take specific measures for data conversion. This practical selection process can avoid implicit conversion and enhance the intention of type conversion in the code.
To convert a number to another type, initialize a new number of the desired type for the existing value. In the following example, the constantTwoThousandIsUInt16The constant one is of the typeUInt8. They cannot be directly added because they have different types. Therefore, this example callsUInt16(One) CreateUInt16And initialize the value of one with the new value (the new value isUInt16) Replaces the original value (the original value isUInt8):
1 let twoThousand: UInt16 = 2_0002 let one: UInt8 = 13 let twoThousandAndOne = twoThousand + UInt16 (one) // change the constantOneInitializeUInt16
Now both sides of the plus sign areUInt16Type, so the addition is valid. Output constant (TwoThousandAndOne).UInt16Because it is twoUInt16The sum of values.
Some types (initial values) are the default methods for calling the Swift Type constructor and passing the initial values. Behind-the-scenes operations are,UInt16There is an acceptanceUInt8Value constructor, so this constructor is used according to the existingUInt8Create a newUInt16. HoweverArbitraryType -- only allowedUInt16Provides the type of constructor. Extend existing types to provide methods for accepting constructors of new types (including custom types). See the extension (which will be translated later) chapter.
Conversions between integers and floating-point numbers
The conversion between integer and floating-point numbers must be explicitly specified:
1 let three = 32 let pointOneFourOneFiveNine = 0.141593 let pi = Double (three) + pointOneFourOneFiveNine4 // pi equals 3.14159, so the inferred type is Double
In the above example, the constantThreeIs used to create a newDoubleType, so that the types on both sides of the plus sign are consistent. If the type is not converted, it is not allowed to be added.
In turn, the conversion from a floating point to an integer is equally feasible, and the integer type can be usedDoubleOrFloatValue initialization:
1 let integerPi = Int (pi) 2 // integerPi is equal to 3, the type is inferred as Int
In this way, when the floating point is initialized as a new integer, the floating point value is always truncated. That is, 4.75 is changed to 4, and-3.9 is changed to-3.
Note:
The combination rules of numeric constants or variables are different from those of numeric literal. Literal 3 can be directly added to literal 0.14159, because literal numbers do not explicitly specify types, and they do not have explicit types themselves. Its type is inferred only when it is evaluated by the compiler.
Thank you, Swifter-QQ group: 362232993 ~
Fork: https://github.com/Joejo/Swift-lesson-for-chinese