简体   繁体   中英

Formatting Bigdecimal. JavaFX

I want to print BigDecimal in TableColumn in my JavaFx application. But I can't properly format it.

I've tried this:

DecimalFormat df = new DecimalFormat("#,###.00");
tc_ma_sell_amount.setCellValueFactory(cellData -> new SimpleStringProperty(df.format(cellData.getValue().getSellAmount()).toString()));

Format is ok, it prints 1 005 689.56. But problem here is when I sort table according to this column in my application, it refers to this values as if they are Strings and sorting is not correct.

I made following changes:

tc_ma_sell_amount.setCellValueFactory(cellData -> new SimpleObjectProperty<BigDecimal>(cellData.getValue().getSellAmount()));

Here, format is not ok. (1005689.5600), but sorting works correctly. What do I have to change in order to have right format("#,###.00") and sorting?

I did it myself.

tc_ma_sell_amount.setCellFactory(param -> {
            return new TableCell<ConversionDeals, BigDecimal>(){
                @Override
                protected void updateItem(BigDecimal item, boolean empty) {
                    super.updateItem(item, empty);
                    if(empty || item == null){
                        setText("");
                    } else {
                        setText(df.format(item));
                    }
                }
            };
        });

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM