This gist will host some nice extensions for anything Kotlin.
/**
* Rounds a [Double] to [numberOfPlaces].
*/
fun Double.roundTo(numberOfPlaces: Int): Double {
return round(this*10.0.pow(numberOfPlaces))/10.0.pow(numberOfPlaces)
}
/**
* Returns the decimal part of a [Double].
*/
val Double.decimal get() = this % toInt()
/**
* Equivalent of [String.isNullOrEmpty] for [Int].
*
* @see [String.isNullOrEmpty]
* @see [String.isNullOrBlank]
*/
fun Int?.isNullOrZero(): Boolean = this == 0 || this == null
/**
* Integrate a function (lambda) from [from] to [to].
*/
fun ((Float) -> Float).integrate(
from: Float,
to: Float,
dx: Float
): Float {
var at = from
var integral = 0f
while(at < to) {
integral += this(at) * dx
at += dx
}
return integral
}