I was experimenting with adding a wrapper around Doubles for percentages, since that's something many other table solutions support.
Something like this works for basic usage:
data class Percentage(val value: Double, val noDecimals: Int = 2) : Number(), Comparable<Percentage> {
override fun toByte(): Byte = value.toInt().toByte()
override fun toDouble(): Double = value
override fun toFloat(): Float = value.toFloat()
override fun toInt(): Int = value.toInt()
override fun toLong(): Long = value.toLong()
override fun toShort(): Short = value.toInt().toShort()
override fun compareTo(other: Percentage): Int = value.compareTo(other.value)
override fun toChar(): Char = value.toInt().toChar()
private fun Double.roundToNoDecimals(noDecimals: Int): Double =
round(this * 10.0.pow(noDecimals)) / 10.0.pow(noDecimals)
override fun toString(): String {
val percentage = value * 100.0
val double =
if (noDecimals <= 0) {
round(percentage).toLong()
} else {
percentage.roundToNoDecimals(noDecimals)
}
return "$double%"
}
}
fun Number.toPercentage(noDecimals: Int = 2): Percentage = Percentage(this.toDouble(), noDecimals)
However, since this is a Number, I was under the impression we supported converting it (among other things) right out of the gate. Unfortunately I was wrong:
To fully support any new Number implementation, we need:
Decide on a "default" number type to convert unknowns to. I suggest Double.
filled in Number in impl/convert.kt::createConverter
I was experimenting with adding a wrapper around Doubles for percentages, since that's something many other table solutions support.
Something like this works for basic usage:
However, since this is a
Number
, I was under the impression we supported converting it (among other things) right out of the gate. Unfortunately I was wrong:To fully support any new
Number
implementation, we need:Double
.Number
in impl/convert.kt::createConverterNumber
cases: https://github.com/Kotlin/dataframe/issues/558Number
columns