I'm a new JavaFX programmer and my question is I'm getting multiple decimal points in the slider and I only want one. I'm getting like 99.99999999999 for each value within but I would love to get only one like 99.9
The thing is printf is not applicable here I guess and I only know that way >.<
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.
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. That will give you the value with a single decimal after the point. Code:
float val = 0.999f;
float valOneDecimal = ((int) (val * 10f)) / 10f;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.