user1971804
user1971804

Reputation: 111

showing floating numbers in TextField in javafx

I am trying to make payment section in my test app.

I have TextField that takes Buttons value (from 0 to 9) and adds it at the end.

TextField pamount;

String value = ((Button)event.getSource()).getText();

    pamount.setText(pamount.getText()+value);

so, if I click buttons from 1 to 9 consecutively, I get

123456789

in TextField

My problem is that I want it to be shown as floating number like

1234567.89

Is there any way to format TextField to hold or show the value like this?

Thanks in advance.

Upvotes: 0

Views: 3628

Answers (2)

James_D
James_D

Reputation: 209553

If you want the button presses to change the value of some number, you should define a variable to hold that number. To avoid repeatedly performing floating point operations (which at some point might accumulate rounding errors), it probably best to let that variable hold the number of cents (or equivalent in your currency), which is an integer.

Additionally, to make it easy for the text field (or label) to always display a text value corresponding to that value, it's convenient to use a JavaFX observable property.

So I would do:

private IntegerProperty totalCents = new SimpleIntegerProperty();

and then you can let your display bind its text to a formatted version of that property:

display.textProperty().bind(totalCents.divide(100.0).asString("$ %.2f"));

Finally, your buttons just have to update the numeric value:

private Button createNumberButton(int value) {
    Button button = new Button(Integer.toString(value));

    button.setOnAction(e -> totalCents.set(totalCents.get() * 10 + value));

    return button ;
}

Here's a SSCCE:

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class PaymentPad extends Application {

    private IntegerProperty totalCents = new SimpleIntegerProperty();

    @Override
    public void start(Stage primaryStage) {
        GridPane pad = new GridPane();
        pad.setHgap(2);
        pad.setVgap(2);

        pad.add(createNumberButton(0), 0, 4);
        for (int i = 1 ; i <= 9 ; i++) {
            int columnIndex = (i-1) % 3;
            int rowIndex = 3 - (i-1) /3 ;
            pad.add(createNumberButton(i), columnIndex, rowIndex);
        }

        Button clearButton = createButton("C");
        clearButton.setOnAction(e -> totalCents.set(0));
        pad.add(clearButton, 1, 4, 2, 1);

        Label display = new Label();
        display.textProperty().bind(totalCents.divide(100.0).asString("$%.2f"));
        display.setMaxWidth(Double.MAX_VALUE);
        display.setAlignment(Pos.CENTER_RIGHT);
        pad.add(display, 0, 0, 3, 1);

        Scene scene = new Scene(pad);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Button createNumberButton(int value) {
        Button button = createButton(Integer.toString(value));
        button.setOnAction(e -> totalCents.set(totalCents.get()*10+value));
        return button ;
    }

    private Button createButton(String text) {
        Button button = new Button(text);
        button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        GridPane.setFillHeight(button, true);
        GridPane.setFillWidth(button, true);
        return button ;     
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 3

Bene
Bene

Reputation: 734

Use DecimalFormat:

DecimalFormat format = new DecimalFormat("#.00"); 
String formattedText = format.format(value);

Upvotes: 1

Related Questions