Swift 國際化貨幣框架 Money,支持不同貨幣之間的計算和轉換。支持比特幣,也可以自定義貨幣類型。
示例代碼:
import Money let money: Money = 100 print("I'll give \(money) to charity.”)以上代碼,根據設置的區(qū)域不同,將輸出不同的結果。
區(qū)域設置為美國時輸出:
I'll give $100.00 to charity區(qū)域設置為英國時輸出:
I'll give £100.00 to charity區(qū)域為中國時輸出:
I'll give CN¥100.00 to charity初始化指定區(qū)域的貨幣:
let dollars = USD(minorUnits: 3250) let yuen = JPY(minorUnits: 3000) print(“You have \(dollars) and \(yuen)”)以上代碼輸出:
You have $32.50 and ¥3,000創(chuàng)建比特幣:
let bitcoin: BTC = 0.1234_5678 print(“You have \(bitcoin)”)以上代碼輸出:
You have ?0.12345678