10

If I have a number that is 100,000,000 how can I represent that as "100M" in a string?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • This is similar, but not quite a duplicate: http://stackoverflow.com/questions/529432/java-format-number-in-millions – Aaron Novstrup Sep 09 '10 at 00:54
  • Here's a class that does something similar: http://jcs.mobile-utopia.com/jcs/5242_ScaledNumberFormat.java , unfortunately it doesn't seem to be part of a supported library. – oksayt Sep 09 '10 at 00:59
  • None of the answers consider i18n! – jwl Jan 29 '14 at 22:44

4 Answers4

8

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.

Community
  • 1
  • 1
Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
3

There is an algorithm to do that:

You need a map that looks like

2 => "hundred"
3 => "thousand"
6 => "million"
9 => "billion"
12 => "trillion"
15 => "quadrillion"

... and so on...

1) Take the number "num", calculate the log10 exponent "ex" of the number and floor it.

Attention

log10(0) doesn't exist so check that the number is not 0 and since it doesn't make sense to output something like 20 = "2 ten" you should return the number as it is if it's smaller than 100 !

2) Now iterate thru the keys of the hash map above and look if a key matches, if not take the key that is smaller than the exponent "ex".

3) Update "ex" to this key!

4) Now format the number like

num = num / pow(10, ex)

(!! ex is a key of the hash map !!)

5) now you could round the number to a certain precision and output num + yourHash[ex]

An example:

number = 12345.45
exponent = floor(log10(12345.45))

exponent should now be 4 !

look for a key in the hash map -- whoops no key matches 4 ! -- so take 3 !

set exponent to 3 

now you scale the number:

number = number / pow(10, exponent)

number = 12345.45 / pow(10, 3) 

number = 12345.45 / 1000

number is now 12.34545

now you get the value to the corresponding key out of the hash map

the value to the key, which is 3 in this example, is thousand  

so you output 12.34545 thousand
sled
  • 14,525
  • 3
  • 42
  • 70
1

Here's my solution to make it a little more generic:

private static final String[] magnitudes = new String[] {"", "K", "M"};

public static String shortenNumber(final Integer num) {
    if (num == null || num == 0) 
        return "0";

    float res = num;
    int i = 0;
    for (; i < magnitudes.length; i++) {
        final float sm = res / 1000;
        if (sm < 1) break;

        res = sm;
    }


    // don't use fractions if we don't have to
    return ( (res % (int) res < 0.1) ?
                String.format("%d", (int)res) :
                String.format("%.1f", res)
            ) 
            + magnitudes[i];
}
phreakhead
  • 14,721
  • 5
  • 39
  • 40
0

This is more general solution.

public static String abbreviateNumber(long num) {

    long temp = num / 1000000; 
    if(temp > 0) {
        return temp + "M+";
    }

    temp = num / 1000;
    if (temp > 0) {
        return temp + "K+";
    }

    temp = num / 500;
    if (temp > 0) {
        return  "500+";
    }

    temp = num / 100;
    if (temp > 0) {
        return  "100+";
    }

    temp = num / 50;
    if (temp > 0) {
        return  "50+";
    }

    temp = num / 10;
    if (temp > 0) {
        return  "10+";
    }

    return String.valueOf(num);
}
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72