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.
然后当选择其中一个值时,它会被添加到最终值。所有加在一起的值是双精度到2位小数。
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,我需要做什么?或者我该怎么做才能获得类似的结果?
4 个解决方案
#1
2
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]
#2
33
You can cast it with Double()
like this
你可以像这样使用Double()来强制转换它
var totalAmount = valueA + valueB + valueC + Double(myValue)
#3
3
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
#4
-1
To convert a float to an integer in Swift. Basic casting like this does not work because these vars are not primitives, unlike floats and ints in Objective-C:
在Swift中将float转换为整数。像这样的基本强制转换不起作用,因为这些变量不是原始变量,与Objective-C中的浮点数和整数不同:
var float:Float = 2.2
var integer:Int = float as Float
#1
2
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]
#2
33
You can cast it with Double()
like this
你可以像这样使用Double()来强制转换它
var totalAmount = valueA + valueB + valueC + Double(myValue)
#3
3
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
#4
-1
To convert a float to an integer in Swift. Basic casting like this does not work because these vars are not primitives, unlike floats and ints in Objective-C:
在Swift中将float转换为整数。像这样的基本强制转换不起作用,因为这些变量不是原始变量,与Objective-C中的浮点数和整数不同:
var float:Float = 2.2
var integer:Int = float as Float