2

I am trying to print the result as a double and receive the following error:-

Exception in thread "main" java.lang.NumberFormatException: For input string: "kotlin.Unit"

How would I fix this?

calculateWeight(150.0).toString().toDouble()
}
fun calculateWeight(bodyWeight: Double) {
    val mercury = bodyWeight * 0.38
    val venus = bodyWeight * 0.91
    val earth = bodyWeight * 1.00
    val mars = bodyWeight * 0.38
    val jupiter = bodyWeight * 2.34
    val saturn = bodyWeight * 1.06
    val uranus = bodyWeight * 0.92
    val neptune = bodyWeight * 1.19
    val pluto = bodyWeight * 0.06

    println("My body weight is $bodyWeight pounds and on the different planets it equals:\n" +
                "Mercury: $mercury, \nVenus: $venus, \nEarth: $earth, " +
                "\nMars: $mars, \nJupiter: $jupiter, \nSaturn: $saturn, \nUranus: $uranus," +
                "\nNeptune: $neptune, \nPluto: $pluto")
SK1dev
  • 1,049
  • 1
  • 20
  • 52

2 Answers2

2

The problem is with this line

calculateWeight(150.0).toString().toDouble()

In calculateWeight function since there is no return type it resolves to default kotlin.Unit which you're trying to convert to Double using toDouble() remove this and it'll work perfectly. And exception happened after method execution because it happened on value returned by function. Hope this helps

Bali
  • 749
  • 6
  • 19
  • Thanks. I had it this way before and on the weight being 150 the results print as a double, however, when I change the weight to say 115 I get the following: My body weight is 115.0 pounds and on the different planets it equals: Mercury: 43.7, Venus: 104.65, Earth: 115.0, Mars: 43.7, Jupiter: 269.09999999999997, Saturn: 121.9, Uranus: 105.80000000000001, Neptune: 136.85, Pluto: 6.8999999999999995 – SK1dev Aug 24 '19 at 08:41
  • @SKdev Floating-point can't store decimal numbers exactly.  See e.g. [this answer](https://stackoverflow.com/questions/588004/is-floating-point-math-broken).  One solution might be to use `BigDecimal` instead. – gidds Aug 24 '19 at 09:04
  • @gidds Thank you, I will take a look at the link. – SK1dev Aug 24 '19 at 10:03
2

Remove the .toString().toDouble() part and then implement it this way to fix the decimal formatting problem:

fun calculateWeight(bodyWeight: Double) {
    val mercury = bodyWeight * 0.38
    val venus = bodyWeight * 0.91
    val earth = bodyWeight * 1.00
    val mars = bodyWeight * 0.38
    val jupiter = bodyWeight * 2.34
    val saturn = bodyWeight * 1.06
    val uranus = bodyWeight * 0.92
    val neptune = bodyWeight * 1.19
    val pluto = bodyWeight * 0.06

    println("My body weight is %.2f pounds and on the different planets it equals:\n" +
            "Mercury: %.2f, \nVenus: %.2f, \nEarth: %.2f, " +
            "\nMars: %.2f, \nJupiter: %.2f, \nSaturn: %.2f, \nUranus: %.2f," +
            "\nNeptune: %.2f, \nPluto: %.2f".format(
                    bodyWeight, mercury, venus, earth, mars,
                    jupiter, saturn, uranus, neptune, pluto))
}

Better yet, here's a more idiomatic way to do it.

enum class Planet(val relativeGravity: Double) {
    Mercury(0.38),
    Venus(0.91),
    Earth(1.00),
    Mars(0.38),
    Jupiter(2.34),
    Saturn(1.06),
    Uranus(0.92),
    Neptune(1.19),
    Pluto(0.06);

    fun convert(weight: Double): Double {
        return weight * relativeGravity
    }
}

fun calculateWeight(bodyWeight: Double, unit: String = "pounds") {
    val prefix = "My body weight is $bodyWeight $unit and on the different planets it equals:\n"
    println(Planet.values().joinToString(prefix = prefix, separator = ",\n") { planet ->
        "%s: %.2f".format(planet.name, planet.convert(bodyWeight))
    })
}
Leo Aso
  • 11,898
  • 3
  • 25
  • 46