Swift 2.0 Study Notes (Day 15)--note the conversion between numeric types

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

in other languages such as C,objective-c , and Java , there are two ways to convert integers:

    • From the small range number to the large range number conversion is automatic;

    • Forcing type conversions from a large number to a small range can result in the loss of data precision.

and in Swift These two methods are not feasible and need to be explicitly converted through some functions, the code is as follows:

Let Historyscore:uint8 = 90

Let englishscore:uint16 = 130

Let totalscore = Historyscore + englishscore// Error


The program will have a compilation error becauseHistoryscoreis aUInt8type, andEnglishscoreis aUInt16types, which cannot be converted between them.

Two methods of conversion:

    • one is to UInt8 of the Historyscore converted to UInt16 type. This conversion is safe because it is converted from a small range to a large range number.

Code:

Let totalscore = UInt16 (historyscore) + englishscore// is the correct method of conversion.

    • The other is to UInt16 of the Englishscore converted to UInt8 type. Because it is converted from a large range to a small range number, this conversion is unsafe, and if the number of conversions is larger it can result in loss of precision.

Code:

Let totalscore = Historyscore + UInt8 (englishscore)// is the correct method of conversion.

in this example Englishscore The value is , this conversion is successful, if the number is changed to 1300, although the program compiles no problem, but the exception information is output in the console.


Conversion between integral type and floating point type

Conversions between integral and floating-point types are similar to conversions between integral types:

Let historyscore:float = 90.6   

Let englishscore:uint16 = 130 

Let totalscore = Historyscore + englishscore// Error

Let totalscore = Historyscore + Float (englishscore)// Right, safe

Let totalscore = UInt16 (historyscore) + englishscore// right, the decimals are cut off  

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.

Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom

More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com

Luxgen Classroom Forum Website: http://51work6.com/forum.php

Swift 2.0 Learning Note (Day 15)--note the conversion between numeric types

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.