 |
Swift3 - อยากทราบวิธีการใช้สัญลักษณ์อะไรสักอย่างแทนด้วย String ... |
|
 |
|
|
 |
 |
|
น่าจะประมาณนี้ครับ
let timeString = String(format: "The current time is %02d:%02d", 10, 4)
|
 |
 |
 |
 |
Date :
2017-03-13 14:36:43 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
let raw$ = [10.1, 20.2, 30.356]
var formal$ = raw$.map({"$" + String(format:"%.2f", $0)})
print(formal$) // ["$10.10", "$20.20", "$30.36"]
|
 |
 |
 |
 |
Date :
2017-03-13 14:37:10 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
import Foundation
extension Int {
func format(f: String) -> String {
return String(format: "%\(f)d", self)
}
}
extension Double {
func format(f: String) -> String {
return String(format: "%\(f)f", self)
}
}
let someInt = 4, someIntFormat = "03"
println("The integer number \(someInt) formatted with \"\(someIntFormat)\" looks like \(someInt.format(someIntFormat))")
// The integer number 4 formatted with "03" looks like 004
let someDouble = 3.14159265359, someDoubleFormat = ".3"
println("The floating point number \(someDouble) formatted with \"\(someDoubleFormat)\" looks like \(someDouble.format(someDoubleFormat))")
// The floating point number 3.14159265359 formatted with ".3" looks like 3.142
|
 |
 |
 |
 |
Date :
2017-03-13 14:45:20 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|