chinwag-android/app/src/main/java/com/keylesspalace/tusky/util/NumberUtils.kt
Nik Clayton 071e00774e
Replace shortNumber() with formatNumber() (#3519)
formatNumber() was existing code to show numbers with suffixes like K, M, etc, so re-use that code and delete shortNumber().

Update the tests to (a) test formatNumber(), and (b) be parameterised.
2023-06-10 16:29:26 +02:00

29 lines
863 B
Kotlin

@file:JvmName("NumberUtils")
package com.keylesspalace.tusky.util
import java.text.NumberFormat
import kotlin.math.abs
import kotlin.math.ln
import kotlin.math.pow
private val numberFormatter: NumberFormat = NumberFormat.getInstance()
private val ln_1k = ln(1000.0)
/**
* Format numbers according to the current locale. Numbers < min have
* separators (',', '.', etc) inserted according to the locale.
*
* Numbers >= min are scaled down to that by multiples of 1,000, and
* a suffix appropriate to the scaling is appended.
*/
fun formatNumber(num: Long, min: Int = 100000): String {
val absNum = abs(num)
if (absNum < min) return numberFormatter.format(num)
val exp = (ln(absNum.toDouble()) / ln_1k).toInt()
// Suffixes here are locale-agnostic
return String.format("%.1f%c", num / 1000.0.pow(exp.toDouble()), "KMGTPE"[exp - 1])
}