import kotlin.math.abs
fun percentChange(x: Double, y: Double): Double {
return ((y - x) / abs(x)) * 100.0
}
fun describeChange(x: Double, y: Double): String {
val pct = percentChange(x, y)
return when {
pct > 0 ->
"From %.1f to %.1f is a %.2f%% increase (+%.2f%%)".format(x, y, pct, pct)
pct < 0 ->
"From %.1f to %.1f is a %.2f%% decrease (%.2f%%)".format(x, y, abs(pct), pct)
else ->
"From %.1f to %.1f is no change".format(x, y)
}
}
fun main() {
println(describeChange(-80.0, 120.0))
println(describeChange(120.0, 30.0))
}
/*
run:
From -80.0 to 120.0 is a 250.00% increase (+250.00%)
From 120.0 to 30.0 is a 75.00% decrease (-75.00%)
*/