Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding BigDecimal Numbers in Kotlin

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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: 

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading