0

All I want to do is Format numbers in compact (abbreviated) notation, like "1.2K" instead of "1200", but I seem to can't figure out how to do that with CompactDecimalFormat

When I do: CompactDecimalFormat.getInstance().parse("1200")
it still gives me "1200" and not "1.2K"

lasec0203
  • 2,422
  • 1
  • 21
  • 36
  • yes possible, The "short" style is also capable of formatting currency amounts, such as "$1.2M" instead of "$1,200,000.00", https://developer.android.com/reference/kotlin/android/icu/text/CompactDecimalFormat, https://developer.android.com/reference/kotlin/android/icu/text/CompactDecimalFormat.CompactStyle – Hasanuzzaman Rana Nov 04 '20 at 03:39
  • CompactDecimalFormat.getInstance(locale: Locale!, style: CompactDecimalFormat.CompactStyle.SHORT).parse("1200"); could work – Hasanuzzaman Rana Nov 04 '20 at 03:42
  • @HasanuzzamanRana that doesn't work, [parse is in-op](https://developer.android.com/reference/kotlin/android/icu/text/CompactDecimalFormat#parse) This functionality hasn't been ported to [Android yet](https://stackoverflow.com/a/54110758/2445763). Guess I will take my pick from the gajillion of hacky solutions [here](https://stackoverflow.com/questions/4753251/how-to-go-about-formatting-1200-to-1-2k-in-java) – lasec0203 Nov 04 '20 at 05:20
  • That should work, as per documentation. then you could follow https://www.iditect.com/how-to/52773332.html – Hasanuzzaman Rana Nov 04 '20 at 12:22

1 Answers1

2
val formattedNumber = CompactDecimalFormat.getInstance(Locale.getDefault(), CompactDecimalFormat.CompactStyle.SHORT).format(12000)
lasec0203
  • 2,422
  • 1
  • 21
  • 36
  • 1
    Hi. Please provide some description to your answer. It will help others as well to understand the problem better. – WhiteSpidy. Nov 29 '20 at 14:06
  • this works. so it looks like I was suppose to being passing the value into the `format` method and not `parse`. – lasec0203 Dec 02 '20 at 04:27