feat: ScriptUtils 加 formatSize/toLong/toInt,脚本中直接用 utils.xxx() 调用

This commit is contained in:
mediabot-pt
2026-07-01 11:11:25 +08:00
parent 343f06cd62
commit 5c44180661
2 changed files with 20 additions and 13 deletions

View File

@@ -132,5 +132,21 @@ public class GroovyScriptEngine {
return null;
} catch (Exception e) { return null; }
}
/** 字节数 → 可读大小 */
public String formatSize(long bytes) {
if (bytes == 0) return "0";
String[] units = {"B","KB","MB","GB","TB"};
double v = bytes;
int i = 0;
while (v >= 1024 && i < units.length - 1) { v /= 1024; i++; }
return String.format("%.2f %s", v, units[i]);
}
/** 字符串转数字 */
public Long toLong(String s) {
try { return Long.parseLong(s.replaceAll("[^\\d-]", "")); } catch (NumberFormatException e) { return null; }
}
public Integer toInt(String s) {
try { return Integer.parseInt(s.replaceAll("[^\\d-]", "")); } catch (NumberFormatException e) { return null; }
}
}
}