Convert a double to 2 decimal places with comma separator

pmaingi

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

 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")));
Elliott Frisch

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Round a double to 2 decimal places

Convert String with Dot or Comma as decimal separator to number in JavaScript

Trim Double to 2 decimal places

Convert a number to 2 decimal places in Java

Convert large number with comma/decimal to Double

Round up double to 2 decimal places

Convert string with thousands (and decimal) separator into double

Best way to convert a double to String without decimal places

Format a decimal with comma as decimal separator and without thousands separator

How to convert floating point decimal separator from dot to comma in Javascript

Format a number with comma separators and round to 2 decimal places in Python 2?

How do I convert a double to 2 decimal places?

decimal separator in woocommerce to be a comma

AngularJS: Change decimal separator to comma

Casting double from petapoco sql request with MS Access decimal separator comma

SwiftUI - Display Double rounded to 2 decimal places

Replace point decimal separator with comma

How to convert a string to decimal with 2 decimal places?

Display 2 decimal places, and use comma as separator in pandas?

Scala: Print Double with decimal and thousands seperator and 2 decimal places

Convert from 4 decimal places to 2

Converting Double to 2 Decimal Places

Convert string to decimal to always have 2 decimal places

Add comma separator to a numbers with 2 decimal points

adding 2 decimal places to Convert.ToString

Jquery/Javascript Regular expression to filter decimal numbers with comma, 13 int places and 2 decimal places

convert string column to double with period as decimal point and thousands separator

Swift Double - divide with 2 decimal places

Double with two Decimal Places