[英]Convert a double to 2 decimal places with comma separator
我認為我有以下代碼
IndexedContainer icLoaded = new IndexedContainer();
icLoaded.removeAllItems();
icLoaded.removeAllContainerFilters();
icLoaded.addContainerProperty("Average Cost", Double.class, null);
在模型中我有以下代碼
public IndexedContainer DoFetchstockCodesTable(String passedSQL,
IndexedContainer passTable, String CustomsaleQuerry) {
try {
Class.forName(dbsettings.dbDriver);
final Connection conn = DriverManager.getConnection(
new Home().GiveMeSessionDB(), dbsettings.dbUsername,
dbsettings.dbPassword);
final Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(passedSQL.trim());
while (rs.next()) {
passTable.addItem(rs.getString("item_id"));
passTable.getContainerProperty(rs.getString("item_id"),
"Average Cost").setValue(rs.getDouble("average_cost"));
我怎么能轉換下面
passTable.getContainerProperty(rs.getString("item_id"),
"Average Cost").setValue(rs.getDouble("average_cost"));
顯示一個帶有2個小數位的數字,用逗號分隔符分隔,如1,000.12使用
NumberFormat numberFormat = new DecimalFormat("#,###.00");
當我使用下面的內容時,不顯示任何內容。
passTable.getContainerProperty(rs.getString("item_id"),
"Average Cost").setValue(numberFormat.format(rs.getDouble("average_cost")));
您調用NumberFormat#format(double)
並將格式化后的double
保存為String
,因為double
是原始值,並且沒有固有格式-
NumberFormat numberFormat = new DecimalFormat("#,###.00");
double val = 1000.12;
System.out.println(numberFormat.format(val));
輸出是
1,000.12
編輯
你需要改變它
icLoaded.addContainerProperty("Average Cost", Double.class, null);
至
icLoaded.addContainerProperty("Average Cost", String.class, null);
您可以通過以下方式設置值的格式:
Table table = new Table() {
@Override
protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
if ("Average Cost".equals(colId)) {
NumberFormat numberFormat = new DecimalFormat("#,###.00");
return numberFormat.format(property.getValue());
}
return super.formatPropertyValue(rowId, colId, property);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.