[英]how to display JTextField in double form, not in the form of a string
例如:
double y = rs.getDouble ("a");
double z = rs.getDouble ("b");
double q = rs.getDouble ("c");
TextField2.setText (String.valueOf ((q * y) + z));
但是如果我拿數據,那么結果是這樣的:3000.0是一個字符串而不是一個雙字符串。
如何以雙精度格式顯示JTextField,而不是以字符串的形式顯示
使用適當的JComponent
帶有Number Formatter的JFormattedTextField
帶有Number Formatter和SpinnerNumberModel的JSpinner
使用JFormattedTextField
NumberFormat amountFormat = NumberFormat.getNumberInstance();
amountFormat.setMinimumFractionDigits(2);
amountFormat.setMaximumFractionDigits(2);
JFormattedTextField textfield1 = new JFormattedTextField(amountFormat);
而不是setText()
,你將使用setValue(Object value)
,所以你會這樣做
double value = q * y + z; // no need for paranthesis - order of operation
textfield2.setValue(new Double(value));
請參閱JFormattedTextField文檔 | Tuorial | 的NumberFormat
注意,如果您想要貨幣轉換,您可以使用。 NumberFormat.getCurrencyInstance()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.