将字符串格式化为KB,MB和GB

时间:2013-08-07 09:29:51

标签: android string

下面是返回String中数据计数器值的类。我想将String格式化为KB,MB和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);

}

这是一个很好的方法:

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);
    }

但我不知道如何在我的onCreate代码中使用上述方法

提前致谢

4 个答案:

答案 0 :(得分:26)

通过传递字节来调用方法。将执行计算后返回的String添加到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];
}

答案 1 :(得分:0)

只需将您从bytes获得的getTotal**Bytes()传递给humanReadableByteCount方法即可。并将si发送为true或false,具体取决于精度(如您想要1KB = 1000字节或1024字节)。

 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);

答案 2 :(得分:0)

只需使用下面显示的数学公式:

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);

并将其传递给函数

humanReadableByteCount(bytes,true);

其中bytes等于TrafficStats.getTotalTxBytes()

希望它有所帮助。 我在堆栈上找到了一些链接link1link2

答案 3 :(得分:0)

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

MB的

((float) Math.round((sizeInBytes / (1024 * 1024)) * 10) / 10)

GB的

((float) Math.round((sizeInBytes / (1024 * 1024 * 1024)) * 10) / 10)