java將流量KB轉換為GB、MB、KB格式


        /**
	 * 轉換流量格式為xxGBxxMBxxKB
	 * @param flow 156165(xxxxxx)
	 */
	public String changeFlowFormat(String flow) {
		Integer flows = Integer.valueOf(flow);
		if (flows > 0 && flows < 1024) {//小於1M
			return flows + "KB";
		} else if (flows >= 1024 && flows < 1048576) {//大於1M小於1G
			int changeM = (int) Math.floor(flows / 1024);//整M數
			int surplusM = (int) Math.floor(flows % 1024);//除M后的余數
			if (surplusM > 0) {//余數大於0KB
				return changeM + "MB" + surplusM + "KB";
			} else {//整M,沒有余數
				return changeM + "MB";
			}
		} else if (flows >= 1048576) {//大於1G
			int changeG = (int) Math.floor(flows / 1048576);//整G數
			int surplusG = (int) Math.floor(flows % 1048576);//除G后的余數
			if (surplusG >= 1024) {//余數大於大於1M
				int changeM = (int) Math.floor(surplusG / 1024);
				int surplusM = (int) Math.floor(surplusG % 1024);
				if (surplusM > 0) {//余數大於0KB
					return changeG + "GB" + changeM + "MB" + surplusM + "KB";
				} else {//整M,沒有余數
					return changeG + "GB" + changeM + "MB";
				}
			} else if (surplusG < 1024 && surplusG > 0) {//余數小於1M,大於0K
				int surplusM = (int) Math.floor(surplusG % 1024);
				return changeG + "GB" + surplusM + "KB";
			} else {
				return changeG + "GB";
			}
		}
		return "暫無數據";
	}

     //js方法

    /**
     * 轉換流量格式為xxGBxxMBxxKB
     * @param flow 156165(xxxxxx)
     */
    function changeFlowFormat(flow) {
        console.log(flow);
        if (flow >= 0 && flow < 1024) {//小於1M
            return flow + "KB";
        } else if (flow >= 1024 && flow < 1048576) {//大於1M小於1G
            var changeM = Math.floor(flow / 1024);//整M數
            var surplusM = Math.floor(flow % 1024);//除M后的余數
            if (surplusM > 0) {//余數大於0KB
                return changeM + "MB" + surplusM + "KB";
            } else {//整M,沒有余數
                return changeM + "MB";
            }
        } else if (flow >= 1048576) {//大於1G
            var changeG = Math.floor(flow / 1048576);//整G數
            var surplusG = Math.floor(flow % 1048576)//除G后的余數
            if (surplusG >= 1024) {//余數大於大於1M
                var changeM = Math.floor(surplusG / 1024);
                var surplusM = Math.floor(surplusG % 1024);
                if (surplusM > 0) {//余數大於0KB
                    return changeG + "GB" + changeM + "MB" + surplusM + "KB";
                } else {//整M,沒有余數
                    return changeG + "GB" + changeM + "MB";
                }
            } else if (surplusG < 1024 && surplusG > 0) {//余數小於1M,大於0K
                var surplusM = Math.floor(surplusG % 1024);
                return changeG + "GB" + surplusM + "KB";
            } else {
                return changeG + "GB";
            }
        }
        return "暫無數據";
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM