Created
July 26, 2019 06:59
-
-
Save jesvs/5727e1c9ff533d030d08b368fe912a10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.math.ln | |
import kotlin.math.pow | |
fun convertToSuffix(x: Double): String { | |
if (x < 1_000) return "$x" | |
val exp = (ln(x) / ln(1_000.0)).toInt() | |
return String.format("%.1f%c", x / 1_000.0.pow(exp), "kmgtpe".toCharArray()[exp - 1]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment