[英]Java/JavaFX slider decimal points
I'm a new JavaFX programmer and my question is I'm getting multiple decimal points in the slider and I only want one.我是一个新的 JavaFX 程序员,我的问题是我在 slider 中有多个小数点,我只想要一个。 I'm getting like 99.99999999999 for each value within but I would love to get only one like 99.9对于其中的每个值,我都会得到 99.99999999999,但我只想得到一个像 99.9 这样的值
The thing is printf is not applicable here I guess and I only know that way >.<事情是 printf 在这里不适用我猜我只知道这样 >.<
private void createView() {
final GridPane radioGridPane = new GridPane();
final Label frequencyLabel = new Label("something");
final Slider sliderFrequency = new Slider(87.9, 107.9, 90);
frequencyLabel.textProperty().bind(sliderFrequency.valueProperty().asString());
radioGridPane.add(frequencyLabel, 2, 1);
radioGridPane.add(sliderFrequency, 3, 1);
this.setCenter(radioGridPane);
radioGridPane.setGridLinesVisible(true);
}
You were actually quite close.你实际上很接近。 You can use asString method with the following signature: javafx.beans.binding.NumberExpressionBase#asString(java.lang.String)
that accepts a format parameter.您可以使用带有以下签名的asString方法:接受格式参数的javafx.beans.binding.NumberExpressionBase#asString(java.lang.String)
。
Example usage:示例用法:
public class FormatBindingSSCCE extends Application {
@Override
public void start(Stage stage) {
final Label frequencyLabel = new Label();
final Slider sliderFrequency = new Slider(87.9, 107.9, 90);
frequencyLabel.textProperty().bind(sliderFrequency.valueProperty().asString("%.1f"));
final GridPane radioGridPane = new GridPane();
radioGridPane.add(frequencyLabel, 2, 1);
radioGridPane.add(sliderFrequency, 3, 1);
final Scene scene = new Scene(radioGridPane);
stage.setScene(scene);
stage.show();
}
}
You can multiply the decimal value by 10, truncate/floor it to an integer and then divide it by 10 again.您可以将十进制值乘以 10,将其截断/取整为 integer,然后再除以 10。 That will give you the value with a single decimal after the point.这将在该点后为您提供一个小数点后的值。 Code:代码:
float val = 0.999f;
float valOneDecimal = ((int) (val * 10f)) / 10f;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.