Fixing kmb formatting

feature/top10
Laurent 7 years ago
parent 8bcfdc8356
commit 30e2b30eb5
  1. 8
      app/src/main/java/net/pokeranalytics/android/ui/graph/AxisFormatter.kt
  2. 91
      app/src/main/java/net/pokeranalytics/android/util/extensions/NumbersExtension.kt
  3. 12
      app/src/test/java/net/pokeranalytics/android/BasicUnitTest.kt

@ -8,11 +8,11 @@ import kotlin.math.roundToInt
class LargeNumberFormatter : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
return value.kmbFormatted
return value.kmbFormatted()
}
override fun getAxisLabel(value: Float, axis: AxisBase?): String {
return value.roundToInt().kmbFormatted
return value.roundToInt().kmbFormatted()
}
}
@ -20,11 +20,11 @@ class LargeNumberFormatter : ValueFormatter() {
class HourFormatter : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
return value.kmbFormatted + "H"
return value.kmbFormatted() + "H"
}
override fun getAxisLabel(value: Float, axis: AxisBase?): String {
val test = value.kmbFormatted + "H"
val test = value.kmbFormatted() + "H"
return test
}

@ -7,70 +7,71 @@ import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.*
val Number.kmbFormatted: String
get() {
var thousandsExponent = 0
var v = this.toDouble()
while (abs(v) >= 10000 && thousandsExponent < 3) {
fun Number.kmbFormatted(threshold: Double = 10000.0): String {
var thousandsExponent = 0
var v = this.toDouble()
if (abs(v) >= threshold) {
while (abs(v) >= 1000 && thousandsExponent < 3) {
v /= 1000
thousandsExponent++
}
}
val unit = when(thousandsExponent) {
0 -> ""
1 -> "K"
2 -> "M"
3 -> "B"
else -> "B+" // shouldn't happen
}
val formatter = NumberFormat.getInstance()
return formatter.format(v) + unit
val unit = when (thousandsExponent) {
0 -> ""
1 -> "K"
2 -> "M"
3 -> "B"
else -> "B+" // shouldn't happen
}
val formatter = NumberFormat.getInstance()
return formatter.format(v) + unit
}
// Double
fun Double.round(): String {
val formatter = DecimalFormat("##.##")
return formatter.format(this)
val formatter = DecimalFormat("##.##")
return formatter.format(this)
}
fun Double.formatted(): String {
val format = NumberFormat.getNumberInstance()
format.maximumFractionDigits = 2
format.minimumFractionDigits = 0
return format.format(this)
val format = NumberFormat.getNumberInstance()
format.maximumFractionDigits = 2
format.minimumFractionDigits = 0
return format.format(this)
}
fun Double.toCurrency(currency: Currency? = null): String {
val currencyFormatter = NumberFormat.getCurrencyInstance()
val currencyFormatter = NumberFormat.getCurrencyInstance()
currency?.let {
currencyFormatter.currency = currency
}
currencyFormatter.maximumFractionDigits = 2
currencyFormatter.minimumFractionDigits = 0
return currencyFormatter.format(this)
currencyFormatter.maximumFractionDigits = 2
currencyFormatter.minimumFractionDigits = 0
return currencyFormatter.format(this)
}
fun Double.toRate(): String {
val currencyFormatter = NumberFormat.getInstance()
currencyFormatter.minimumFractionDigits = 0
currencyFormatter.maximumFractionDigits = 6
return currencyFormatter.format(this)
val currencyFormatter = NumberFormat.getInstance()
currencyFormatter.minimumFractionDigits = 0
currencyFormatter.maximumFractionDigits = 6
return currencyFormatter.format(this)
}
fun Double.formattedHourlyDuration() : String {
return (this * 1000 * 3600).toLong().toMinutes()
fun Double.formattedHourlyDuration(): String {
return (this * 1000 * 3600).toLong().toMinutes()
}
// Return the time from minutes to hours:minutes
fun Int.toMinutes(context: Context) : String {
val hours = this / 60
val minutesLeft = this % 60
var duration = ""
fun Int.toMinutes(context: Context): String {
val hours = this / 60
val minutesLeft = this % 60
var duration = ""
if (hours < 1) {
duration += "$minutesLeft ${context.getString(if (minutesLeft > 1) R.string.mins else R.string.min)}"
@ -80,17 +81,17 @@ fun Int.toMinutes(context: Context) : String {
duration += if (minutesLeft < 10) "0$minutesLeft" else minutesLeft.toString()
}
return duration
return duration
}
// Return the time from milliseconds to hours:minutes
fun Long.toMinutes() : String {
val totalMinutes = this / (1000 * 60)
val hours = totalMinutes / 60
val minutesLeft = totalMinutes % 60
var duration = ""
duration += hours.toString()
duration += ":"
duration += if (minutesLeft < 10) "0$minutesLeft" else minutesLeft.toString()
return duration
fun Long.toMinutes(): String {
val totalMinutes = this / (1000 * 60)
val hours = totalMinutes / 60
val minutesLeft = totalMinutes % 60
var duration = ""
duration += hours.toString()
duration += ":"
duration += if (minutesLeft < 10) "0$minutesLeft" else minutesLeft.toString()
return duration
}

@ -19,13 +19,15 @@ class BasicUnitTest : RealmUnitTest() {
val n3 = n2 * n2 // 1M
val n4 = n3 * n2 // 1B
val s1 = n1.kmbFormatted
val s2 = n2.kmbFormatted
val s3 = n3.kmbFormatted
val s4 = n4.kmbFormatted
val s1 = n1.kmbFormatted()
val s2 = n2.kmbFormatted()
val s2b = n2.kmbFormatted(1000.0)
val s3 = n3.kmbFormatted()
val s4 = n4.kmbFormatted()
Assert.assertEquals("100", s1)
Assert.assertEquals("1K", s2)
// Assert.assertEquals("1/e000", s2) // weird error Expected :1 000, Actual :1 000
Assert.assertEquals("1K", s2b)
Assert.assertEquals("1M", s3)
Assert.assertEquals("1B", s4)

Loading…
Cancel
Save