/**
* 입력받은 값을 파일 단위별로 계산하여 읽기 쉽게 변환해준다.
* @author kimtaekyung
*
*/
public class FileCapacityCalculationReadOnly {
private static final long K = 1024;
private static final long M = K * K;
private static final long G = M * K;
private static final long T = G * K;
/**
*
* @param value
* @return
*/
public static String convertToObjectRepresentation(Object value){
String result = null;
String stringToConvert = String.valueOf(value);
Long convertedLong = Long.parseLong(stringToConvert);
final long[] dividers = new long[] { T, G, M, K, 1 };
final String[] units = new String[] { "TB", "GB", "MB", "KB", "B" };
if(convertedLong < 1) {
result = "none";
return result;
}
for(int i = 0; i < dividers.length; i++){
final long divider = dividers[i];
if(convertedLong >= divider){
result = format(convertedLong, divider, units[i]);
break;
}
}
return result;
}
/**
*
* @param value
* @param divider
* @param unit
* @return
*/
private static String format(final long value,final long divider,final String unit){
final double result = divider > 1 ? (double) value / (double) divider : (double) value;
return String.format("%.1f %s", Double.valueOf(result), unit);
}
}
댓글 없음:
댓글 쓰기