0

Forgive my lack of knowledge, as I am correcting code to give me the correct numbers from a database. I am normally using java, but was given this file in .kt


    val Time: Date,
    val duration: Long,
    val rate: Long
){
    constructor():this(Date(),0,0)
}
@Composable
fun SessionContainer(sessionList: MutableList<MSession>) {
    var totalM = 0f
    var totalMi = 0f
    var mSessions = 0
    sessionList.forEach {
        if (it.completionTime.time < Calendar.getInstance().time.time)
            totalM += it.rate * (it.duration / 1000 / 60 / 60)
        else{
            totalMi += it.rate * (it.duration / 1000 / 60 / 60)
            mSessions++
        }
    }
    Column(
        Modifier
            .fillMaxWidth()
            .padding(12.dp)
    ) {
        Text(text = "Total M [${totalM.toDouble()}]", fontWeight = FontWeight.Bold)

        Text(text = "Total Mi [${totalMi.toDouble()}] in total $mSessions sessions", fontWeight = FontWeight.Bold)
    }
}

The information in the database is in decimal format. I just need to be able to display it and I do not know how to code kotlin. I made a change already that is now displays "0.0" instead of "0", but there should be a number with those decimals(i.e. 123.4567)

  • Check this post, maybe will help you. https://stackoverflow.com/questions/53848189/format-number-using-decimal-format-in-kotlin – Rafael Moreira Feb 02 '23 at 01:10

1 Answers1

0

In the code, totalM and totalMi are float variables. If you want to display the decimal value, you need to format the value to a string with a specific number of decimal places.

Here's how you can format the decimal value with 4 decimal places:

Text(text = "Total M [${String.format("%.4f", totalM)}]", fontWeight = FontWeight.Bold)
Text(text = "Total Mi [${String.format("%.4f", totalMi)}] in total $mSessions sessions", fontWeight = FontWeight.Bold)