只是想知道.NET是否提供了一种干净的方法来执行此操作:
int64 x = 1000000;
string y = null;
if (x / 1024 == 0) {
y = x + " bytes";
}
else if (x / (1024 * 1024) == 0) {
y = string.Format("{0:n1} KB", x / 1024f);
}
Run Code Online (Sandbox Code Playgroud)
等等...
我正在重构一些旧代码并遇到以下代码行将字节转换为GB.
decimal GB = KB / 1024 / 1024 / 1024;
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来重构下面的代码?
更新
我的意思是说千兆字节的字节数.我提供了错误的信息.
下面是返回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: " + …Run Code Online (Sandbox Code Playgroud)