stackData.map{"\($0)"}
joined(separator: "\n")
print(String(repeating: "2", count: 0))
이렇게 해도 오류가 나지 않는다
(self as NSString).doubleValue
let x = 6.5
print(x.rounded(.toNearestOrAwayFromZero))
print(x.rounded(.towardZero))
print(x.rounded(.up))
print(x.rounded(.down))
// 7.0
// 6.0
// 7.0
// 6.0
print(practiceCode.sum(value: 0.001, repeat: 1000))
// 1.0000000000000007
컴퓨터가 가지는 한계로 실수를 계산할때는 자릿수와 오차 범위를 고려해야한다
struct Stack<T> {
private var items = [T]()
func isEmpty() -> Bool {
return items.isEmpty
}
mutating func push(_ element: T) {
self.items.append(element)
}
mutating func pop() -> T? {
return items.popLast()
}
func peek() -> T? {
return items.last
}
func size() -> Int? {
return items.count
}
}
extension Stack:CustomStringConvertible {
var description: String {
let topDivider = "-------Stack--------\n"
let bottomDivider = "\n-------Stack--------\n"
let stackElementData = items.map{"\($0)"}.reversed().joined(separator: "\n")
return topDivider.appending(stackElementData).appending(bottomDivider)
}
}
let x = 8.625
print(x / 0.75)
// Prints "11.5"
let q = (x / 0.75).rounded(.towardZero)
// q == 11.0
let r = x.truncatingRemainder(dividingBy: 0.75)
// r == 0.375
let x1 = 0.75 * q + r
// x1 == 8.625
let someValue = 5
let someOptional: Int? = nil
switch someOptional {
case .some(someValue):
println("the value is \(someValue)")
case .some(let val):
println("the value is \(val)")
default:
println("nil")
}
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumSignificantDigits = 18
let price = 12345678.12345678
let result = numberFormatter.string(for: price)!