/**
* @Author: liyue
* @Date: 2022/04/22/00:17
* @Description:
*/
public class MathUtil {
/**
* 根据小k计算存储
*
* @param kb
* @return
*/
public static String countStorageDescribe(Long kb) {
if (kb == null) {
return "";
}
double res = round(kb / 1024.0 / 1024 / 1024 / 1024);
if (res > 1) {
return res + "TB";
}
res = round(kb / 1024.0 / 1024 / 1024);
if (res > 1) {
return res + "GB";
}
res = round(kb / 1024.0 / 1024);
if (res > 1) {
return res + "MB";
}
res = round(kb / 1024.0);
if (res > 1) {
return res + "KB";
}
return kb + "kb";
}
public static String countHaoshi(Long timeMillis) {
if (timeMillis == null) {
return "";
}
double res = round(timeMillis / 1000.0 / 60 / 60 / 24 / 30 / 12);
if (res > 1) {
return res + "年";
}
res = round(timeMillis / 1000.0 / 60 / 60 / 24 / 30);
if (res > 1) {
return res + "月";
}
res = round(timeMillis / 1000.0 / 60 / 60 / 24);
if (res > 1) {
return res + "日";
}
res = round(timeMillis / 1000.0 / 60 / 60);
if (res > 1) {
return res + "时";
}
res = round(timeMillis / 1000.0 / 60);
if (res > 1) {
return res + "分";
}
res = round(timeMillis / 1000.0);
if (res > 1) {
return res + "秒";
}
return timeMillis + "毫秒";
}
private static Double round(Double d){
return DoubleUtil.round(d,2);
}
public static void main(String[] args) {
System.out.println(countHaoshi(1000*60*60*2L));
}
}
DoubleUtil: