【问题标题】:Format String into KB, MB and GB将字符串格式化为 KB、MB 和 GB
【发布时间】:2018-08-03 00:34:04
【问题描述】:

下面是返回字符串中数据计数器值的类。我想将 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 代码中使用上述方法

提前致谢

【问题讨论】:

    标签: android string


    【解决方案1】:

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

    【讨论】:

    • @user574639 “计数器运行得非常快”是什么意思?应用程序运行的速度与 Chiranjib 的方法无关,这是正确的。
    【解决方案2】:

    只需在需要的地方使用如下所示的一些数学公式:

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

    其中字节等于 TrafficStats.getTotalTxBytes()

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

    【讨论】:

      【解决方案3】:

      只需将您从getTotal**Bytes() 获得的bytes 传递给humanReadableByteCount 方法。并将si 发送为真或假,具体取决于精度(比如您想要 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);
      

      【讨论】:

      • 你真的能把它显示到代码中吗?我正在编辑有问题的代码。
      • 感谢 user2550754。我明白了......愚蠢的变化它是
      • 如果它有效,那么您应该接受答案并投票。这将有助于将来访问者找到答案。
      【解决方案4】:

      知识库:((float) Math.round((sizeInBytes / (1024)) * 10) / 10)

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

      国标:((float) Math.round((sizeInBytes / (1024 * 1024 * 1024)) * 10) / 10)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-02
        • 2012-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-28
        相关资源
        最近更新 更多