function change(limit){ var size = ""; if(limit < 0.1 * 1024){ //小於0.1KB,則轉化成B size = limit.toFixed(2) + "B" }else if(limit < 0.1 * 1024 * 1024){ //小於 ...
function change(limit){ var size = ""; if(limit < 0.1 * 1024){ //小於0.1KB,則轉化成B size = limit.toFixed(2) + "B" }else if(limit < 0.1 * 1024 * 1024){ //小於0.1MB,則轉化成KB size = (limit/1024).toFixed(2) + "KB" }else if(limit < 0.1 * 1024 * 1024 * 1024){ //小於0.1GB,則轉化成MB size = (limit/(1024 * 1024)).toFixed(2) + "MB" }else{ //其他轉化成GB size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB" } var sizeStr = size + ""; //轉成字元串 var index = sizeStr.indexOf("."); //獲取小數點處的索引 var dou = sizeStr.substr(index + 1 ,2) //獲取小數點後兩位的值 if(dou == "00"){ //判斷後兩位是否為00,如果是則刪除00 return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2) } return size; } var newLimit = change(1048576); console.log(newLimit);