Skip to content

Support additional Number types (like Percentage) #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Jolanrensen opened this issue Feb 29, 2024 · 0 comments
Open

Support additional Number types (like Percentage) #610

Jolanrensen opened this issue Feb 29, 2024 · 0 comments
Labels
enhancement New feature or request good first issue Good issues to pick-up for newcomers research This requires a deeper dive to gather a better understanding
Milestone

Comments

@Jolanrensen
Copy link
Collaborator

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)

image_720

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:

image_720

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
  • Check statistics for Number cases: Describe breaks on Number column #558
  • Column Arithmatics for Number columns
@Jolanrensen Jolanrensen added enhancement New feature or request invalid This issue/PR doesn't seem right research This requires a deeper dive to gather a better understanding labels Feb 29, 2024
@Jolanrensen Jolanrensen added this to the Backlog milestone Feb 29, 2024
@zaleslaw zaleslaw removed the invalid This issue/PR doesn't seem right label Apr 8, 2024
@zaleslaw zaleslaw added the good first issue Good issues to pick-up for newcomers label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good issues to pick-up for newcomers research This requires a deeper dive to gather a better understanding
Projects
None yet
Development

No branches or pull requests

2 participants