官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 文水县| 西乌珠穆沁旗| 湘西| 璧山县| 武冈市| 丹寨县| 龙胜| 东乌珠穆沁旗| 莒南县| 江川县| 青河县| 托克托县| 邵阳县| 瑞丽市| 景东| 剑川县| 安顺市| 景东| 巧家县| 牡丹江市| 吉水县| 梁山县| 阿坝县| 和平县| 乌鲁木齐市| 鄂托克前旗| 奉新县| 上犹县| 台南县| 九寨沟县| 竹溪县| 乌兰浩特市| 鹰潭市| 清新县| 榆树市| 克山县| 杭锦旗| 南漳县| 鲁甸县| 顺平县| 柳河县|