Kotlin: Pluralize amount of currency

103 Views Asked by At

I want to write some code to determine if I need to write "Quarter" or "Quarters" depending on the amount.

Up to now I have the following code:

fun main(args: Array<String>) {
    var changeDue = 1.22;
    Change(changeDue);
}

fun Change(changeDue: Double) {
    var quarters = Math.floor(changeDue / 1.0);
    print(Math.round(quarters * 4) / 1)
    if (quarters <= 2)
        println(" Quarters")
    else if (quarters > 2)
        println(" Quarter")
    println("")
    println(".22 remain")
}

Is it correct?

Is there a better way to achieve it?

1

There are 1 best solutions below

2
Louis Wasserman On

It looks like you just have the logic backwards: it should be Quarter if quarters <= 1 and Quarters otherwise.