ios 你如何在swift中将int转换为double?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26579912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:22:37  来源:igfitidea点击:

How do you convert int to double in swift?

iosswiftios8

提问by Don Gitman

I've looked at the answers for converting int's to floats and other similar answers but they don't do exactly what I want.

我已经查看了将 int 转换为浮点数和其他类似答案的答案,但它们并没有完全满足我的要求。

I'm trying to create a basic program that takes a number does some different calculations onto it and the results of those calculations are added together at the end.

我正在尝试创建一个基本程序,该程序接受一个数字对其进行一些不同的计算,最后将这些计算的结果加在一起。

For one of those calculations I created a segmented controller with the 3 different values below

对于其中一个计算,我创建了一个分段控制器,其中包含以下 3 个不同的值

 var myValues: [Double] = [0.00, 1.00, 1.50]
 var myValue = [myValuesSegmentedController.selectedSegmentIndex]

then when one of those values is picked, it's added to the final value. All the values added together are Doubles to 2 decimal places.

then when one of those values is picked, it's added to the final value. 所有加在一起的值都是双精度到小数点后两位。

var totalAmount = valueA + valueB + valueC + myValue

the problem I'm having is that swift won't let me add "myValue" to those final calculations. It gives me the error:

我遇到的问题是 swift 不允许我在这些最终计算中添加“myValue”。它给了我错误:

Swift Compiler Error. Cannot invoke '+' with an argument list of type '($T7, @lvalue [int])'

What do I need to do to change that value to a Double? Or what can I do to get a similar result?

我需要做什么才能将该值更改为 Double?或者我该怎么做才能得到类似的结果?

采纳答案by Leo Dabus

The problem is you are trying to add an array instead of an Int, so You don't even need to convert anything, considering that all of your values are already Doubles and your index actually has to be an Int. So

问题是你试图添加一个数组而不是一个 Int,所以你甚至不需要转换任何东西,考虑到你的所有值都已经是双精度值并且你的索引实际上必须是一个 Int。所以

 let myValues = [0.00, 1.00, 1.50]
 let myValue = [myValuesSegmentedController.selectedSegmentIndex] // your mistake is  here, you are creating one array of integers with only one element(your index)

The correct would be something like these:

正确的应该是这样的:

let myValues = [0.00, 1.00, 1.50]
let totalAmount = myValues.reduce(0, combine: +) + myValues[myValuesSegmentedController.selectedSegmentIndex]

回答by Amadan

You can cast it with Double()like this

你可以Double()像这样投射它

var totalAmount = valueA + valueB + valueC + Double(myValue)

回答by Steve Rosenberg

Put this in a playground:

把它放在操场上:

var myValues: [Double] = [0.00, 1.00, 1.50]
let valueA = 1
let valueB = 2
let valueC = 3
var totalAmount = Double(valueA + valueB + valueC) + myValues[2]
println(totalAmount)  //output is 7.5

valueA/B/C are all inferred to be Int.

valueA/B/C 都被推断为 Int。

totalAmount is inferred to be a Double

totalAmount 被推断为 Double

回答by Rich Mariano

To convert a floatto an integerin Swift. Basic casting like this does not work because these vars are not primitives, unlike floatsand intsin Objective-C:

于转换浮动到一个整数斯威夫特。像这样的基本铸造不起作用,因为这些增值经销商都没有原语,不像花车整数的Objective-C

var float:Float = 2.2
var integer:Int = float as Float