1

Is it possible to parse BigDecimal from a textfield ? I have seen some code where they parse bigdecimal from strings. But is it also possible to use this on textfield. I have worked out some code to make it work. But i have no idea if i should do it this way.

Float price = Float.parseFloat(textFieldInvoiceServicePrice.getText());
String servicetext = textFieldInvoiceService.getText();
BigDecimal priceExcl=  BigDecimal.valueOf(price);
Greg
  • 1,690
  • 4
  • 26
  • 52
  • possible duplicate of [Safe String to BigDecimal conversion](http://stackoverflow.com/questions/3752578/safe-string-to-bigdecimal-conversion) – Ali Cheaito Mar 16 '15 at 13:52

2 Answers2

1

No, that's not how you should do it - you're currently parsing from a string to float, then converting that float to a BigDecimal. You clearly know how to get a string from a text field, because you're already doing it in the first line - you can just do that with BigDecimal instead:

BigDecimal price = new BigDecimal(textFieldInvoiceServicePrice.getText());

However, you should note that this won't be culture-sensitive - it will always use . as the decimal separator, for example. You should use NumberFormat if you want culture-sensitive parsing.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Here's an example with a property, a textfield and a converter:

public class MoneyParser extends Application {

    ObjectProperty<BigDecimal> money = new SimpleObjectProperty<BigDecimal>();

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        TextField textField = new TextField();

        // bind to textfield
        Bindings.bindBidirectional(textField.textProperty(), moneyProperty(), new MoneyStringConverter());

        // logging
        money.addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> System.out.println("Money: " + newValue));

        root.getChildren().addAll(textField);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

    public class MoneyStringConverter extends StringConverter<BigDecimal> {

        DecimalFormat formatter;

        public MoneyStringConverter() {
            formatter = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.US));
            formatter.setParseBigDecimal(true);
        }

        @Override
        public String toString(BigDecimal value) {

            // default
            if( value == null)
                return "0";

            return formatter.format( (BigDecimal) value);

        }

        @Override
        public BigDecimal fromString(String text) {

            // default
            if (text == null || text.isEmpty())
                return new BigDecimal( 0);

            try {

                return (BigDecimal) formatter.parse( text);

            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }
    }

    public final ObjectProperty<BigDecimal> moneyProperty() {
        return this.money;
    }

    public final java.math.BigDecimal getMoney() {
        return this.moneyProperty().get();
    }

    public final void setMoney(final java.math.BigDecimal money) {
        this.moneyProperty().set(money);
    }

}
Roland
  • 18,114
  • 12
  • 62
  • 93
  • It might be nice to have JavaFX property wrappers for BigDecimal and the [JavaMoney API](https://java.net/projects/javamoney/pages/Home), rather than using a double to represent money - of course no such thing currently exists. – jewelsea Mar 16 '15 at 17:28
  • Oh, you're right. I changed it to Object and BigDecimal. – Roland Mar 16 '15 at 17:52