我有一个绑定到两个属性的标签.第一个值(DeletedFileCountProperty)是一个简单的int,无需格式化.但是,如何将第二个属性(SimpleLongProperty)格式化为人类可读的filesize值?
例子:
我可以调用绑定中的humanReadableByteCount
函数来设置值的格式吗?
迄今为止我的代码:
public class MainController implements Initializable {
private final SimpleIntegerProperty deletedFilesCount = new SimpleIntegerProperty();
private final SimpleLongProperty deletedFilesSize = new SimpleLongProperty();
@FXML
Label deletedFilesLabel;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
deletedFilesLabel.textProperty().bind(Bindings.format("Deleted files: %d (%d)", deletedFilesCountProperty(), deletedFilesSizeProperty()));
}
/**
* formats a long number to a human readable file size value
* returns something like: 2MB or 4GB and so on instead of very long Long values.
*/
public static String humanReadableByteCount(long 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");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
}
谢谢