使用逗号分隔符将double转换为2位小数

pma*_*ngi 2 java vaadin7

在我看来,我有以下代码

IndexedContainer icLoaded = new IndexedContainer(); 
icLoaded.removeAllItems();  
icLoaded.removeAllContainerFilters();

icLoaded.addContainerProperty("Average Cost", Double.class, null);
Run Code Online (Sandbox Code Playgroud)

在模型中我有以下代码

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"));
Run Code Online (Sandbox Code Playgroud)

我怎么能转换下面

 passTable.getContainerProperty(rs.getString("item_id"),
                    "Average Cost").setValue(rs.getDouble("average_cost"));
Run Code Online (Sandbox Code Playgroud)

显示一个带有2个小数位的数字,用逗号分隔符分隔,如1,000.12使用

 NumberFormat numberFormat = new DecimalFormat("#,###.00");
Run Code Online (Sandbox Code Playgroud)

当我使用下面的内容时,不显示任何内容.

 passTable.getContainerProperty(rs.getString("item_id"),
                    "Average Cost").setValue(numberFormat.format(rs.getDouble("average_cost")));
Run Code Online (Sandbox Code Playgroud)

Ell*_*sch 5

您调用NumberFormat#format(double)并保存格式double为a String,因为a double是原始值,并且没有固有的格式 -

NumberFormat numberFormat = new DecimalFormat("#,###.00");
double val = 1000.12;
System.out.println(numberFormat.format(val));
Run Code Online (Sandbox Code Playgroud)

输出是

1,000.12
Run Code Online (Sandbox Code Playgroud)

编辑

你需要改变它

icLoaded.addContainerProperty("Average Cost", Double.class, null);
Run Code Online (Sandbox Code Playgroud)

icLoaded.addContainerProperty("Average Cost", String.class, null);
Run Code Online (Sandbox Code Playgroud)