【发布时间】:2013-02-11 17:49:01
【问题描述】:
我不明白为什么这不起作用: (方法取自 SO 上的HERE)。
private String MakeSizeHumanReadable(int bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
String hr = String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
hr = hr.replace("-1 B", "n.a.");
return hr;
}
这一个都不是:-
private String MakeSizeHumanReadable(int bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
String hr = String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
Pattern minusPattern = Pattern.compile("-1 B");
Matcher minusMatcher = minusPattern.matcher(hr);
if (minusMatcher.find()) {
return "n.a.";
} else {
return hr;
}
}
我不时从请求中得到-1 B(这是正常的),这在n.a. 中永远不会改变(....我的问题)。
有人有想法吗?
【问题讨论】:
-
考虑首先告诉我们你想用这段代码做什么。
-
感谢负 1。我刚刚编辑以添加来自 SO 的链接。你已经很轻了。无论如何,
from time to time I get -1 B from the request (this is normal), that is never changed in n.a. (...my question).行解释说我想更改从n.a.中的链接方法返回的-1 B -
很公平。我在想这两个块的共同代码可以解释一下。
标签: java android string substitution string-substitution