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 trialJialong Zhang
9,821 PointsFind 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
8,429 PointsAll 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
9,821 PointsI 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
8,429 PointsNice work! That looks much nicer, and much more simple. Great job.
Maria Te
8,521 PointsI 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
9,821 PointsI think you need to import UIKit
Maria Te
8,521 PointsOh okay thanks, that worked.
Jialong Zhang
9,821 PointsJialong Zhang
9,821 Pointsthank you. It also works without if let statement.
Simon Di Giovanni
8,429 PointsSimon Di Giovanni
8,429 PointsYouβre welcome! Glad I could help.