Skip to content

Instantly share code, notes, and snippets.

@jesvs
Created July 26, 2019 06:59
Show Gist options
  • Save jesvs/5727e1c9ff533d030d08b368fe912a10 to your computer and use it in GitHub Desktop.
Save jesvs/5727e1c9ff533d030d08b368fe912a10 to your computer and use it in GitHub Desktop.
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