- iOS 12 Programming for Beginners
- Craig Clayton
- 245字
- 2021-07-02 15:04:52
Adding floating-point numbers
Now let's add floating-point numbers, using the let constant, in Playground:
let gradeAvg = 2.9
let version:Float = 1.1
This is demonstrated in the following screenshot:

You will notice that a couple of things are different. First, we are using the let keyword. Using let tells our program that this is a constant. Constants are variables that cannot change once they are set (as opposed to a non-constant variable, which can change after being set).
The other thing you might have noticed is that we explicitly set our version to Float. When dealing with a floating-point number, it can be a Double or a Float. Without getting too technical, a Double is much more precise than a Float. The best way to explain this is to use pi as an example. Pi is a number in which the digits go on forever. Well, we cannot use a number that goes on forever; however, a Double and Float handle how precise that number is. Let's look at the following diagram to see what I mean by precise:

So, in the preceding example, you can see that Float only displays 3.14, whereas Double gives you a much more accurate number. In Swift, a Double is preferred. Therefore, if you do not explicitly set the floating-point number to a Float, Swift defaults to a Double. To set version to a Float, you must purposely set it that way.
- Modern JavaScript Web Development Cookbook
- Application Development with Qt Creator(Second Edition)
- Proxmox High Availability
- Go Web Scraping Quick Start Guide
- 互聯網安全的40個智慧洞見:2014年中國互聯網安全大會文集
- Wireshark網絡分析就這么簡單
- 物聯網之霧:基于霧計算的智能硬件快速反應與安全控制
- WordPress Web Application Development
- Kong網關:入門、實戰與進階
- Unity Artificial Intelligence Programming
- 4G小基站系統原理、組網及應用
- 物聯網與智能家居
- 高級網絡技術
- 工業以太網技術:AFDX/TTE網絡原理、接口、互連與安全
- 物聯網