1

I have looked and can not figure out how to format my TableColumn using a Double. It always ends up like 234.23232033 etc etc. I would like to to show 234.23 for example. I have seen post similar to mine, but I just cannot figure how to integrate it into my code. Partly my fault for maybe being to far ahead of what I actually know. But I would like to figure this out. Thank you.

Here is part of my Controller...

@FXML
private TableView<CheckBookData> tableView;
@FXML
private TableColumn<CheckBookData, String> transactionCol;
@FXML
private TableColumn<CheckBookData, Double> balanceCol;

ObservableList<CheckBookData> checkbook = 
FXCollections.observableArrayList();

Double balance = 0.0;

public void initialize(URL url, ResourceBundle rb)
{
    transactionCol.setCellValueFactory(new 
    PropertyValueFactory<CheckBookData, String>("transaction"));

    balanceCol.setCellValueFactory(new PropertyValueFactory<CheckBookData, 
            Double>("balance"));
}

private void handleAddData(ActionEvent event)
{
    Double deposit = Double.parseDouble(depositField.getText());

    balance = balance + deposit;

    checkbook.add(new CheckBookData(transaction, withdraw, deposit, 
    balance, checkNumber, date));
}

Here is part of my CheckBookData class...

public class CheckBookData
{
private SimpleStringProperty checkNumber, transaction, date;
private SimpleDoubleProperty withdraw, deposit, balance;

public CheckBookData(String transaction, Double withdraw, Double deposit,
        Double balance, String checkNumber, String date)
{
    this.transaction = new SimpleStringProperty(transaction);
    this.withdraw = new SimpleDoubleProperty(withdraw);
    this.deposit = new SimpleDoubleProperty(deposit);
    this.balance = new SimpleDoubleProperty(balance);
    this.checkNumber = new SimpleStringProperty(checkNumber);
    this.date = new SimpleStringProperty(date);
}
public Double getBalance()
{
    return balance.get();
}

public void setBalance(SimpleDoubleProperty balance)
{
    this.balance = balance;
}
chad
  • 35
  • 1
  • 7
  • This may also help [Best way to Format a Double value to 2 Decimal places](https://stackoverflow.com/questions/8819842/) – Slaw Jan 14 '19 at 19:45
  • Thank you very much! Don't know how I missed that. – chad Jan 14 '19 at 20:10

1 Answers1

0

You have to format the value, not the table - just clearing.
You can apply some math to this problem:

double balance = <INSERT_VALUE>;
balance = balance*100;
balance = Math.round(balance);
balance = balance/100;

You multiply by 100, round it, and then divide it by 100. This should give you a formatted result.

Alexander Falk
  • 499
  • 8
  • 21