Convert Double to String in swift

Source: Internet
Author: User

I have been using swift for several days. It is found that in addition to several basic types of conversions, some special numeric types need to be converted from "bridging" to "Objective-C ~

The Code is also very simple ~

Var numString = "1.0" var numDouble: DoublenumDouble = String. bridgeToObjectiveC (numString )(). doubleValue // equivalent to objective-c's "numdouble = [numString doubleValue]" // numDouble value: 1.0

What if numString is not a "Number" character but another character?

Var numString = "abc" var numDouble: DoublenumDouble = String. bridgeToObjectiveC (numString) (). doubleValue // numDouble: 0.0

This will not cause compiler exceptions. Continue with the following situations:

Var numString1 = "1.5abc" var numString2 = "2. abc0 "var numString3 =" abc3.0 "var numDouble: DoublenumDouble = String. bridgeToObjectiveC (numString1 )(). doubleValue // numDouble1 value: 1.5 numDouble = String. bridgeToObjectiveC (numString2 )(). doubleValue // numDouble2 value: 2.0 numDouble = String. bridgeToObjectiveC (numString3 )(). doubleValue // numDouble3: 0.0

We can see that the doubleValue index starts from the first character of the string and searches for consecutive numeric characters (including a ". "), to the second". "or non-numeric characters until the end of the index.

 

 

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.