我是 Xcode 的新手,正在尝试制作一个计算毛利的简单应用。
我正在尝试使用以下代码,但它返回值“0”。
有什么想法吗?
// Playground - noun: a place where people can play
import UIKit
var costPrice = 10
var salePrice = 100
var grossProfit = ((salePrice - costPrice) / salePrice) * 100
println(grossProfit)
最佳答案
Apple 免费出版的 iBook“Swift 简介”的前几页对此进行了解释。
Swift 是类型安全的,会从上下文推断类型。
行 var costPrice = 10
推断变量 costPrice
是一个 int。
然后您不能将整数与其他类型的数字(例如 double )隐式组合。
如果你试试这个..
let costPrice = 10.0
let salePrice = 100.0
let grossProfit = ((salePrice - costPrice) / salePrice) * 100.0
你会发现这行得通。
关于swift - Swift 中的基础数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26890394/