[英]Convert a double to 2 decimal places with comma separator
In my view i have the below code 我认为我有以下代码
IndexedContainer icLoaded = new IndexedContainer();
icLoaded.removeAllItems();
icLoaded.removeAllContainerFilters();
icLoaded.addContainerProperty("Average Cost", Double.class, null);
In the model i have the below code 在模型中我有以下代码
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"));
How can i convert the below 我怎么能转换下面
passTable.getContainerProperty(rs.getString("item_id"),
"Average Cost").setValue(rs.getDouble("average_cost"));
to display a number with 2 decimal places separated by a comma separator like 1,000.12 using 显示一个带有2个小数位的数字,用逗号分隔符分隔,如1,000.12使用
NumberFormat numberFormat = new DecimalFormat("#,###.00");
When i use the below , nothing gets displayed . 当我使用下面的内容时,不显示任何内容。
passTable.getContainerProperty(rs.getString("item_id"),
"Average Cost").setValue(numberFormat.format(rs.getDouble("average_cost")));
You call NumberFormat#format(double)
and save the formatted double
as a String
, because a double
is a primitive value and it has no inherent formatting - 您调用
NumberFormat#format(double)
并将格式化后的double
保存为String
,因为double
是原始值,并且没有固有格式-
NumberFormat numberFormat = new DecimalFormat("#,###.00");
double val = 1000.12;
System.out.println(numberFormat.format(val));
Output is 输出是
1,000.12
Edit 编辑
You need to change this 你需要改变它
icLoaded.addContainerProperty("Average Cost", Double.class, null);
to 至
icLoaded.addContainerProperty("Average Cost", String.class, null);
You can format the value in this way: 您可以通过以下方式设置值的格式:
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.