'How can I create a function to return the century from the year in Swift?
I have a problem in code fights. I am getting an error:
declaration is only valid at file scope (extension Decimal)
Can someone please guide me on how to fix this? BTW I am creating a function to return the century with the year as an input. If you have any recommendations for my code let me know.
func centuryFromYear(year: Int) -> Int {
let centuryOtherStart = year / 100
let centuryStart = Double(year / 100)
let centuryEnd = round(centuryStart)
var wholeNumber : Bool
if wholeNumber == true {
return Int(centuryStart)
} else {
return Int(centuryEnd + 1)
}
extension Decimal {
var isWholeNumber: Bool {
wholeNumber = self.isZero || (self.isNormal && self.exponent >= 0)
}
}
}
Solution 1:[1]
Beside the fact that you can't declare an extension inside a function, you need to extend FloatingPoint instead of Decimal. Just add a new Swift file to your project and add the extension there:
extension FloatingPoint {
var isWholeNumber: Bool {
return isZero ? true : isNormal ? self == rounded() : false
}
}
Regarding in your method to extract the year century there are a few mistakes. First you should only divide by 100 after converting your year to Double. Second you need to return the rounded if the result is whole number otherwise return it without rounding and incrementing one:
func centuryFrom(year: Int) -> Int {
let centuryStart = Double(year)/100
return centuryStart.isWholeNumber ? Int(round(centuryStart)) : Int(centuryStart) + 1
}
Testing:
centuryFrom(year: 1801) // XIX
centuryFrom(year: 1900) // XIX
centuryFrom(year: 1901) // XX
centuryFrom(year: 2000) // XX
centuryFrom(year: 2001) // XXI
centuryFrom(year: 2100) // XXI
centuryFrom(year: 2101) // XXII
Solution 2:[2]
Solution is very simple:
func centuryFromYear(year: Int) -> Int {
return (year + 99) / 100
}
Solution 3:[3]
I might be late. But try this one too
func centuryFromYear(year: Int) -> Int {
if year % 100 == 0 {
//this will return exact century like 2000 means 20th Century
return year / 100
}else {
//this will return exact century like 2001 means 20th Century
return year / 100 + 1
}
}
Solution 4:[4]
There's a couple of ways,
you can do this
Op1
func century(_ year: Double) -> String {
var century = Int((year / 100).rounded(.up))
if century != 21 {
return "\(century)th century"
} else {
return "21st century"
}
}
or Op2
func century(_ year: Int) -> String {
switch year {
case 901...1000: return "10th century"
case 1001...1100: return "11th century"
case 1101...1200: return "12th century"
case 1201...1300: return "13th century"
case 1301...1400: return "14th century"
case 1401...1500: return "15th century"
case 1501...1600: return "16th century"
case 1601...1700: return "17th century"
case 1701...1800: return "18th century"
case 1801...1900: return "19th century"
case 1901...2000: return "20th century"
case 2001...2100: return "21st century"
default: return ""
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Senocico Stelian |
| Solution 3 | Majid Bashir |
| Solution 4 | Marlhex |
