Home > database >  How do I link Methods/Functions in Kotlin?
How do I link Methods/Functions in Kotlin?

Time:12-02

I have tried getting this to work a few different ways but just cant seem to wrap my head around it. In Version 1 it tells me that changeDue is not used, and in Version 2 it just runs blank. I'm using code playground if this changes anything Any help is appreciated.

Version 1:

fun main(args: Array<String>) {
var changeDue = 1.23;
fun truncate(changeDue: Double): Double{
var numberOfQuarters = changeDue /.25;

println(numberOfQuarters);

var remainingChange = changeDue - numberOfQuarters * .25;
println(remainingChange);
return changeDue
}
}

Version 2: Tried using a class

fun main(args: Array<String>) {
    class quarters{
    var changeDue = 1.23;
    fun truncate(changeDue: Double): Double{
    var numberOfQuarters = changeDue /.25;

    println(numberOfQuarters);

    var remainingChange = changeDue - numberOfQuarters * .25;
    println(remainingChange);
    return changeDue
    }
}
}

CodePudding user response:

A couple of remarks to your code:

  • Use val whenever possible instead of var. Having variables that do not change their value after being set the first time will help you avoid common mistakes / bugs in your code.
  • In Kotlin there is no need to use ; at the end of each statement.

Now back to your question. In your code, you were not calling truncate() anywhere. Simple declaring the function does not call it. The following works as expected:

import kotlin.math.floor

fun main(args: Array<String>) {
    fun truncate(changeDue: Double): Double {
        val numberOfQuarters: Double = floor(changeDue / 0.25)
        println(numberOfQuarters)

        val remainingChange = changeDue - numberOfQuarters * 0.25
        println(remainingChange)

        return changeDue
    }

    println(truncate(1.23))
}

However, the output is:

4.0
0.22999999999999998
1.23

As you can see 0.22999999999999998 is not the exact result you were expecting. So that we avoid this you need to use BigDecimal instead of Double as follows:

import java.math.BigDecimal
import kotlin.math.floor

fun main(args: Array<String>) {
    fun truncate(changeDue: BigDecimal): BigDecimal {
        val numberOfQuarters: BigDecimal = floor(changeDue.toDouble() / 0.25).toBigDecimal()
        println(numberOfQuarters)

        val remainingChange = changeDue - numberOfQuarters * 0.25.toBigDecimal()
        println(remainingChange)

        return changeDue
    }

    println(truncate(1.23.toBigDecimal()))
}

Which will return:

4.0
0.230
1.23

Another possibility would be to use a monetary library as the one discussed in https://www.baeldung.com/java-money-and-currency.

CodePudding user response:

You were never calling relevant class/function

Try this:

fun main(args: Array<String>) {
    fun truncate(changeDue: Double): Double{
        var numberOfQuarters = changeDue /.25;

        println(numberOfQuarters);

        var remainingChange = changeDue - numberOfQuarters.toInt() * .25;
        println(remainingChange);
        return changeDue
    }
    println(truncate(1.23))
}

ref

  • Related