JavaFX 格式在 TableColumn 中加倍



我已经看过了,但无法弄清楚如何使用双精度格式化我的表列。它总是像 234.23232033 等结束。例如,我想显示 234.23。我看过与我的类似的帖子,但我只是不知道如何将其集成到我的代码中。部分原因是我的错,因为可能远远领先于我实际知道的。但我想弄清楚这一点。谢谢。

这是我的控制器的一部分...

@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));
}

这是我的CheckBookData课程的一部分...

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;
}

您必须格式化值,而不是表格 - 只是清除。
你可以对这个问题应用一些数学:

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

你乘以 100,四舍五入,然后除以 100。这应该会给你一个格式化的结果。

最新更新