From 26735d2f8860819016b3acacae56e002aeafb7f6 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 25 Mar 2023 19:00:41 +0530 Subject: [PATCH] Use CompactDecimalFormat to format counts. --- .../libretube/extensions/FormatShort.kt | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/github/libretube/extensions/FormatShort.kt b/app/src/main/java/com/github/libretube/extensions/FormatShort.kt index 1c88769e8..4b6c45f4d 100644 --- a/app/src/main/java/com/github/libretube/extensions/FormatShort.kt +++ b/app/src/main/java/com/github/libretube/extensions/FormatShort.kt @@ -1,14 +1,22 @@ package com.github.libretube.extensions +import android.icu.text.CompactDecimalFormat +import android.os.Build +import java.util.* import kotlin.math.pow fun Long?.formatShort(): String { - this ?: return (0).toString() - val units = arrayOf("", "K", "M", "B", "T") - - for (i in units.size downTo 1) { - val step = 1000.0.pow(i.toDouble()) - if (this > step) return String.format("%3.0f%s", this / step, units[i]).trim() + val value = this ?: 0 + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + CompactDecimalFormat + .getInstance(Locale.getDefault(), CompactDecimalFormat.CompactStyle.SHORT) + .format(value) + } else { + val units = arrayOf("", "K", "M", "B", "T") + for (i in units.size downTo 1) { + val step = 1000.0.pow(i.toDouble()) + if (value > step) return String.format("%3.0f%s", value / step, units[i]).trim() + } + value.toString() } - return this.toString() }