feat: ScriptUtils 加 formatSize/toLong/toInt,脚本中直接用 utils.xxx() 调用
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user