Android bytes与KB、MB、GB单位转换 | 您所在的位置:网站首页 › 102400kb等于多少m › Android bytes与KB、MB、GB单位转换 |
1.先定义三个整型常量
//定义GB的计算常量 private static final int GB = 1024 * 1024 *1024; //定义MB的计算常量 private static final int MB = 1024 * 1024; //定义KB的计算常量 private static final int KB = 1024; 2.实现转换public static String bytes2kb(long bytes){ //格式化小数 DecimalFormat format = new DecimalFormat("###.0"); if (bytes / GB >= 1){ return format.format(bytes / GB) + “GB”; } else if (bytes / MB >= 1){ return format.format(bytes / MB) + “MB”; } else if (bytes / KB >= 1){ return format.format(bytes / KB) + “KB”; }else { return bytes + “B”; } } |
CopyRight 2018-2019 实验室设备网 版权所有 |