0

I'm trying to figure out how to reformat a long value that I get from an API. The long value represents a stock's market cap so naturally it varies, but essentially what I would like is to round up and abbreviate.

I've found on here a code to abbreviate the number down depending on how high it is, but I can't figure out how to keep decimals.

For example if I query "NFLX" I get back "117173403648". With the code I would get back 117B. What I would like the output to be is "117.17B".

I have tried Math.round() function before I abbreviate the number, but it does not seem to work the way I want it to, nor does DecimalFormat. Surely I'm using both wrong.

here is the code I found and modified on SO:

    public static String abbreviateNumber(long marketCap) {

        long temp = marketCap / 1000000000;
        if (temp > 0) {
            return temp + "B";
        }

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

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

        return String.valueOf(marketCap);
    }

Using this the output of marketCap is 117B, but I would like to have 2 decimals, and still keep this abbreviation method.

Thanks, any help is greatly appreciated. I'm new to programming and figuring it out as I go.

ADSquared
  • 207
  • 3
  • 12

4 Answers4

1

Use DecimalFormat and double

DecimalFormat format = new DecimalFormat("#0.00B");
long marketCap = 117173403648L;
double value = marketCap / 1000000000.0;
System.out.println(format.format(value));

117.17B

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
1

How about something like this:

public static String abbreviateNumber(long marketCap) {
    int len = String.valueOf(marketCap).length();
    return len > 9 ? String.format("%.2f", marketCap/Math.pow(10, 9))+"B" :
           len > 6 ? String.format("%.2f", marketCap/Math.pow(10, 6))+"M" :
           len > 3 ? String.format("%.2f", marketCap/Math.pow(10, 3))+"K" :
           String.valueOf(marketCap);
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

You should use DecimalFormat class present in java.text package.

Your code would look like this:

public static String abbreviateNumber(long marketCap) {


    double temp = marketCap / 1000000000; 
    DecimalFormat f = new DecimalFormat("##.##");

    if (temp > 0) {
        return f.format(marketCap / 1000000000d) + "B";
    }

    temp = marketCap / 1000000;
    if (temp > 0) {
        return f.format(marketCap / 1000000d) + "M";
    }

    temp = marketCap / 1000;
    if (temp > 0) {
        return f.format(marketCap / 1000d) + "K";
    }

    return String.valueOf(marketCap);
}

note

Not optimal & clean solution. Just updated the answer to working one.

miiiii
  • 1,580
  • 1
  • 16
  • 29
  • It works, as long as the marketCap is in the billions the way you have it. If the long is in the millions like this: 4857729 the return will be 0,00B. – ADSquared Oct 01 '19 at 10:04
  • #MickDostie ya.. right.. same should have happened with @Joakim Danielson answer.. as it is just pointing to 'B' abbrv. only. If the same logic is applied for other abbrv. then it should also break. not sure. Will update my answer. – miiiii Oct 01 '19 at 10:27
  • https://stackoverflow.com/questions/4753251/how-to-go-about-formatting-1200-to-1-2k-in-java answers my question, as pointed out by Alexey. Thanks for trying though, much appreciated. – ADSquared Oct 01 '19 at 10:36
0

With a little change to the code where the division happens, decimals retention can be achieved. And on top that String.format("%.2f", input) can be used for decimal clipping.

public static String abbreviateNumber(long marketCap) {


    //for billion -1000 million above
    double temp = ((double)(marketCap / 1000000000.0));
    if (temp > 1) {
        return String.format("%.2f", temp) + "B";
    }

    //for million formatting
    temp = ((double)(marketCap / 1000000.0));
    if (temp > 1) {
        return String.format("%.2f", temp) + "M";
    }

    //for thousands formatting
    //if K representation only when it reaches at least 1K make temp > 1
    temp = ((double)(marketCap / 1000.0));
    if (temp > 0) {
        return String.format("%.2f", temp) + "K";
    }

    //this will not be reached unless a negative number
    return String.valueOf(marketCap);
}

For "117173403648l" as input:

117.17B

ambianBeing
  • 3,449
  • 2
  • 14
  • 25