user45678
user45678

Reputation: 1514

Format String into KB, MB and GB

Below is the class which returns me data counter value in String. I was wanted to format String into KB, MB, and GB

public class MainActivity extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView infoView = (TextView)findViewById(R.id.traffic_info);
        String info = "";

        long getmobilerxbytes = TrafficStats.getMobileRxBytes();
        long getmobilerxpackets = TrafficStats.getMobileRxPackets();
        long getmobiletxbytes = TrafficStats.getMobileTxBytes();
        long getmobiletxpackets = TrafficStats.getMobileTxPackets();

        long totalrxbytes = TrafficStats.getTotalRxBytes();
        long totalrxpackets = TrafficStats.getTotalRxPackets(); 
        long totaltxbytes = TrafficStats.getTotalTxBytes(); 
        long totaltxpackets = TrafficStats.getTotalTxPackets();

        info += "Mobile Interface:\n";
        info += ("\tReceived: " + getmobilerxbytes + " bytes / " + getmobilerxpackets + " packets\n");
        info += ("\tTransmitted: " + getmobiletxbytes + " bytes / " + getmobiletxpackets + " packets\n");

        info += "All Network Interface:\n";
        info += ("\tReceived: " + totalrxbytes + " bytes / " + totalrxpackets + " packets\n");
        info += ("\tTransmitted: " + totaltxbytes + " bytes / " + totaltxpackets + " packets\n");

        infoView.setText(info);

}

Here is beautiful method to do that :

public static String humanReadableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

But I am not sure how can i use above method in my onCreate code

Thanks in advance

Upvotes: 5

Views: 15247

Answers (4)

chiranjib
chiranjib

Reputation: 5308

Call the method by passing the bytes . Add the String which is returned after doing the calculations to the TextView.

public static String getFileSize(long size) {
    if (size <= 0)
        return "0";

    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(size) / Math.log10(1024));

    return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

Upvotes: 40

surhidamatya
surhidamatya

Reputation: 2609

Just use some mathematical formula shown below where you need:

if(bytes < 1024)
    return bytes + " bytes";

bytes /= 1024;
if(bytes < 1024)
    return bytes + " KB";

bytes /= 1024;
if(bytes < 1024)
    return bytes + " MB";

bytes /= 1024;
if(bytes < 1024)
    return bytes + " GB";

return String(bytes);

and to pass it to function

humanReadableByteCount(bytes,true);

where bytes equals TrafficStats.getTotalTxBytes()

Hope it helps. I found some link on stack too link1 and link2

Upvotes: 1

Vinay W
Vinay W

Reputation: 10190

for KB: ((float) Math.round((sizeInBytes / (1024)) * 10) / 10)

for MB: ((float) Math.round((sizeInBytes / (1024 * 1024)) * 10) / 10)

for GB: ((float) Math.round((sizeInBytes / (1024 * 1024 * 1024)) * 10) / 10)

Upvotes: 0

user2550754
user2550754

Reputation: 905

Simply pass the bytes you get from getTotal**Bytes() to humanReadableByteCount method. And send si as true or false, depending on precision(like you want 1KB=1000 bytes or 1024 bytes).

 public class MainActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView infoView = (TextView)findViewById(R.id.traffic_info);
    String info = "";

    info += "Mobile Interface:\n";
    info += ("\tReceived: " + TrafficStats.getMobileRxBytes() + " bytes / " + TrafficStats.getMobileRxPackets() + " packets\n");
    info += ("\tTransmitted: " + TrafficStats.getMobileTxBytes() + " bytes / " + TrafficStats.getMobileTxPackets() + " packets\n");

    info += "All Network Interface:\n";
    info += ("\tReceived: " + TrafficStats.getTotalRxBytes() + " bytes / " + TrafficStats.getTotalRxPackets() + " packets\n");

    info += humanReadableByteCount(TrafficStats.getTotalRxBytes(),true);

    info += ("\tTransmitted: " + TrafficStats.getTotalTxBytes() + " bytes / " + TrafficStats.getTotalTxPackets() + " packets\n");

    info += humanReadableByteCount(TrafficStats.getTotalTxBytes(),true);

    infoView.setText(info);

Upvotes: 0

Related Questions