Swift學習——Swift基礎詳解(三)

來源:互聯網
上載者:User

標籤:swift   swift基礎   

小葵花課堂繼續開講


Numeric Literals    數字文本

數字文本有以下幾種寫法:

A decimal number, with no prefixA binary number, with a 0b prefixAn octal number, with a 0o prefixA hexadecimal number, with a 0x prefix
十進位數,無首碼;位元,0b首碼;八位元,0o首碼;十六進位數,0x首碼

論數字17的N種表現形式:

let decimalInteger = 17let binaryInteger = 0b10001       // 17 in binary notationlet octalInteger = 0o21           // 17 in octal notationlet hexadecimalInteger = 0x11     // 17 in hexadecimal notation
也可以用十六進位和十進位表示浮點數,科學計數法,十進位用e表示10的指數,十六進位用p表示2的指數

For decimal numbers with an exponent of exp, the base number is multiplied by 10exp:1.25e2 means 1.25 × 102, or 125.0.1.25e-2 means 1.25 × 10-2, or 0.0125.
For hexadecimal numbers with an exponent of exp, the base number is multiplied by 2exp:0xFp2 means 15 × 22, or 60.0.0xFp-2 means 15 × 2-2, or 3.75.
浮點數12.1875的幾種表現形式:

let decimalDouble = 12.1875let exponentDouble = 1.21875e1let hexadecimalDouble = 0xC.3p0
數字文本可以使用其他的一些格式讓數字變得更加易讀,比如添加0或者底線,不會影響數位值(這個功能不錯):

let paddedDouble = 000123.456let oneMillion = 1_000_000let justOverOneMillion = 1_000_000.000_000_1


Numeric Type Conversion    數值類型轉換

通常情況下就使用int,當有其他需要,比如效能或者記憶體需要的時候可以使用別的整數型別,這個時候使用明確大小的類型可以協助你捕獲異常的溢出



相關文章

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.