优化常用单位、用户自定义列保存优化、业务流量库优化

This commit is contained in:
gaoyutao
2025-11-05 14:48:02 +08:00
parent e9970b94a9
commit d0ac03ffbf
5 changed files with 81 additions and 12 deletions
@@ -113,4 +113,56 @@ public class UnitChangeUtil {
}
}
/**
* 次数单位换算
* @param count
* @return
*/
public static String formatWithUnit(long count) {
String[] units = {"", "", "百万", "亿"};
double[] divisors = {1, 10_000.0, 1_000_000.0, 100_000_000.0};
int unitIndex = 0;
double value = count;
for (int i = divisors.length - 1; i >= 0; i--) {
if (count >= divisors[i]) {
unitIndex = i;
value = count / divisors[i];
break;
}
}
// 根据数值大小决定小数位数
String formatPattern = (value >= 100 || unitIndex == 0) ? "%.0f %s次" : "%.1f %s次";
return String.format(formatPattern, value, units[unitIndex]).trim();
}
/**
* 简洁版本:所有值都显示为KB及以上单位
*/
public static String formatWithKbMin(long bytes) {
if (bytes == 0) {
return "0.00 KB";
}
final String[] units = {"KB", "MB", "GB", "TB"};
double value = bytes / 1024.0;
int unitIndex = 0;
while (value >= 1024 && unitIndex < units.length - 1) {
value /= 1024;
unitIndex++;
}
// 四舍五入到两位小数
double roundedValue = Math.round(value * 100.0) / 100.0;
// 处理四舍五入后可能出现的情况
if (roundedValue == 0.0 && value > 0) {
roundedValue = 0.01; // 最小值显示为0.01而不是0.00
}
return String.format("%.2f %s", roundedValue, units[unitIndex]);
}
}