我对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)发布于 2014-11-12 15:25:34
所有这些都是在iBook“Swift简介”的前几页中解释的,这是免费的,由苹果公司出版。
Swift是类型安全的类型,并将从上下文中推断类型。
行var costPrice = 10推断变量costPrice是一个int。
这样,您就不能隐式地将int与其他类型的数字(例如双倍)组合在一起。
如果你试试这个..。
let costPrice = 10.0
let salePrice = 100.0
let grossProfit = ((salePrice - costPrice) / salePrice) * 100.0你会发现这行得通。
发布于 2014-11-12 15:25:22
10和100是整数,所以costPrice和salePrice是整数。整数除法就像你看到的那样被截断。您想在这里使用10.0和100.0。
https://stackoverflow.com/questions/26890394
复制相似问题