英文:
Adding BigDecimal Numbers in Kotlin
问题
Here's the translation of the code section you provided:
我有以下映射,但现在我需要将百分比数据添加到列表中,以便创建公式,我需要列表中所有值的总和
data class Data(
val id: Int,
val cost: Int,
val env: String,
val item: String
)
data class TotalCost(
val item: String,
val env: String,
val cost: BigDecimal
)
val input = listOf(
Data(id = 1, cost = 0.00000000001, env = "prod", item = "Storage"),
Data(id = 2, cost = 0.00000000002, env = "qa", item = "Storage"),
Data(id = 3, cost = 0.00000000003, env = "prod", item = "Storage"),
Data(id = 4, cost = 0.00000000005, env = "qa", item = "Bandwidth"),
Data(id = 5, cost = 0.00000000002, env = "qa", item = "Bandwidth"),
Data(id = 6, cost = 0.00000000005, env = "prod", item = "Bandwidth"),
Data(id = 7, cost = 0.00000000006, env = "prod", item = "Bandwidth"),
Data(id = 8, cost = 0.00000000001, env = "prod", item = "Bandwidth"),
Data(id = 9, cost = 0.00000000005, env = "qa", item = "vm"),
Data(id = 10, cost = 0.00000000002, env = "uat", item = "vm"),
Data(id = 11, cost = 0.00000000003, env = "qa", item = "vm"),
Data(id = 12, cost = 0.00000000004, env = "prod", item = "vm"),
Data(id = 13, cost = 0.00000000005, env = "uat", item = "vm")
)
val result = input
.groupBy {
it.item to it.env
}
.map { (key, value) ->
listOf(
key.first,
key.second,
value.sumOf { it.cost.toBigDecimal() }
)
}
我正在尝试这样做;
var totalCost: BigDecimal = BigDecimal.ZERO
result.forEach { totalCostData ->
totalCost = totalCost.add(totalCostData[2].toBigDecimal())
}
并且收到以下错误消息;
未解析的引用。由于接收器类型不匹配,以下候选项都不适用:
Please note that the code you provided has HTML entities (e.g., "
) which need to be replaced with their respective characters in a real Kotlin environment. This code snippet should be used as a reference, and you might need to adjust it to your specific Kotlin development environment to resolve the error.
英文:
I have the following mapping, but now I need to add percentage data to the list, so to create the formula, I need the total of all the values in the list
data class Data(
val id: Int,
val cost: Int,
val env: String,
val item: String
)
data class TotalCost(
val item: String,
val env: String,
val cost: BigDecimal
)
val input = listOf(
Data(id = 1, cost = 0.00000000001, env = "prod", item = "Storage"),
Data(id = 2, cost = 0.00000000002, env = "qa", item = "Storage"),
Data(id = 3, cost = 0.00000000003, env = "prod", item = "Storage"),
Data(id = 4, cost = 0.00000000005, env = "qa", item = "Bandwidth"),
Data(id = 5, cost = 0.00000000002, env = "qa", item = "Bandwidth"),
Data(id = 6, cost = 0.00000000005, env = "prod", item = "Bandwidth"),
Data(id = 7, cost = 0.00000000006, env = "prod", item = "Bandwidth"),
Data(id = 8, cost = 0.00000000001, env = "prod", item = "Bandwidth"),
Data(id = 9, cost = 0.00000000005, env = "qa", item = "vm"),
Data(id = 10, cost = 0.00000000002, env = "uat", item = "vm"),
Data(id = 11, cost = 0.00000000003, env = "qa", item = "vm"),
Data(id = 12, cost = 0.00000000004, env = "prod", item = "vm"),
Data(id = 13, cost = 0.00000000005, env = "uat", item = "vm")
)
val result = input
.groupBy {
it.item to it.env
}
.map { (key, value) ->
listOf(
key.first,
key.second,
value.sumOf { it.cost.toBigDecimal() }
)
}
I am trying this;
var totalCost: BigDecimal = BigDecimal.ZERO
result.forEach { totalCostData ->
totalCost = totalCost.add(totalCostData[2].toBigDecimal())
}
and getting this error message;
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
答案1
得分: 0
问题位于这里:
listOf(
key.first,
key.second,
value.sumOf { it.cost.toBigDecimal() }
)
您需要将项目转换为3个不同的值,因此将它们存储在列表中。但是,前两个值是字符串,第三个是BigDecimal
。列表只能存储相同类型的项目,因此在这种情况下它变成了List<Any>
,然后totalCostData[2]
就是Any
(这并不完全正确)。
有两种方法可以解决这个问题。您可以将其强制转换为BigDecimal
,因为我们知道它的类型:
totalCost = totalCost.add(totalCostData[2] as BigDecimal)
或者更好的做法是不使用列表,而是将结果存储在一个自定义和完全类型化的类中。事实上,看起来您已经创建了这样的类,但出于某种原因没有使用它:
TotalCost(
key.first,
key.second,
value.sumOf { it.cost.toBigDecimal() }
)
...
totalCost = totalCost.add(totalCostData.cost)
英文:
Problem is located here:
listOf(
key.first,
key.second,
value.sumOf { it.cost.toBigDecimal() }
)
You needed to transform items into 3 distinct values, so you stored them in a list. However, first two values are strings and the third is BigDecimal
. List can only store items of same types, so in this case it becomes pretty much List<Any>
and then totalCostData[2]
is just Any
(this is not exactly correct).
There are two ways to fix the problem. You can either cast it to BigDecimal
as we know its type:
totalCost = totalCost.add(totalCostData[2] as BigDecimal)
Or better, do not use a list, but store results in a custom and fully typed class. As a matter of fact, it seems you already created such class, but for some reason you didn't use it:
TotalCost(
key.first,
key.second,
value.sumOf { it.cost.toBigDecimal() }
)
...
totalCost = totalCost.add(totalCostData.cost)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论