Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Jialong Zhang
Jialong Zhang
9,821 Points

Find PI to the Nth Digit - Swift

Find PI to the Nth Digit - Enter a number and have the program generate PI up to that many decimal places. Keep a limit to how far the program will go.

My solution work with return type String. Is there a way to solve the problem with return type Double? func piToN(to n: Int) -> Double { // I don't know how}

    func piToN(to n: Int) -> String {
        let pi = String(Double.pi)
        var result: String = ""
        if(n < 1 || n > 14){
            print("Please input range from 1 to 14")
        }else{
            result = String(pi.prefix(n+2)) 
//  I don't know why this line does not work.  -->  result = pi.index(pi.startIndex, offsetBy: n)
            print(result)
        }
        return result
    }

piToN(to: 11)

5 Answers

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

All you need to do is type cast it to a Double, using an if let statement as it might not work.

func piToN(to n: Int) -> Double {
    let pi = String(Double.pi)
    var stringResult: String = ""
    var doubleResult: Double = 0.0
    if(n < 1 || n > 14){
        print("Please input range from 1 to 14")
    }else{
        stringResult = String(pi.prefix(n+2))

        print(stringResult)
    }

    if let tempResult = Double(stringResult) {
        doubleResult = tempResult
    } else {
        return doubleResult
    }

    return doubleResult
}

This successfully returns a double value.

Note that I have defaulted the doubleResult to 0.0. If you want, you could add error code to ensure it's clear whats happening if the function returns 0.0.

Jialong Zhang
Jialong Zhang
9,821 Points

thank you. It also works without if let statement.

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

You’re welcome! Glad I could help.

Jialong Zhang
Jialong Zhang
9,821 Points

I finally figure out the new way :)

    func piToN(to n: Int) -> Double {
        let factor = (pow(10, n) as NSDecimalNumber).doubleValue
        let piResult: Double = Double(round(Double.pi * factor) / factor)
        if(n < 1 || n > 14){
            print("Please input range from 1 to 14")
        }else{
            print(piResult)
        }
        return piResult
    }

  piToN(to: 10) // output:  3.1415926536
Simon Di Giovanni
Simon Di Giovanni
8,429 Points

Nice work! That looks much nicer, and much more simple. Great job.

I am just saying this, I tried Zhang's code on my Xcode but it gave me an error saying that the "pow" and "round" in your code doesn't exist. Where did you get it from? Is it from another program you made? And what's Simon's answer for this question?

Jialong Zhang
Jialong Zhang
9,821 Points

I think you need to import UIKit

Oh okay thanks, that worked.