If I have a number that is 100,000,000 how can I represent that as "100M" in a string?
To my knowledge there's no library support for abbreviating numbers, but you can easily do it yourself:
NumberFormat formatter = NumberFormat.getInstance();
String result = null;
if (num % 1000000 == 0 && num != 0) {
result = formatter.format(num / 1000000) + "M";
} else if (num % 1000 == 0 && num != 0) {
result = formatter.format(num / 1000) + "K";
} else {
result = formatter.format(num);
}
Of course, this assumes that you don't want to shorten a number like 1,234,567.89. If you do, then this question is a duplicate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With