Format Double in cell Table

Hi,

I’m trying to format a table cell with a double in the European format (comma for decimals).

This is the code:


		Table table = new Table(){
		@Override
		protected String formatPropertyValue(Object rowId, Object colId,Property property) {
			                Object v = property.getValue();
			                if (v instanceof Double) {
			                	DecimalFormat df= new DecimalFormat("#,00");
			                    return df.format(v);
			                }
			                return super.formatPropertyValue(rowId, colId, property);
			            }
			
			   };
		table.setContainerDataSource(id);
		
		return table;

Something is wrong but i don’t know what!! This with date works fine!!

Thanks!!

DecimalFormat is Locale specific so you could do something like:

DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(getLocale());
df.applyPattern("#.00");
return df.format(v);

Done but no works, the date works fine but amount wrong

		Table table = new Table(){
		@Override
		protected String formatPropertyValue(Object rowId, Object colId,Property property) {
			                Object v = property.getValue();
			                if (v instanceof Double){
			                	DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(getLocale());
			                	df.applyPattern("#,00"); //Comma in Europe
			                	return df.format(v);
			                }else if (v instanceof Date) {
			                	SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			                	return df.format(v);
			                }
			                return super.formatPropertyValue(rowId, colId, property);
			            }
			   };

		   
		table.setContainerDataSource(id);
		return table;
	}

What Locale is your application using? By default getLocale() will return the preferred locale of the user using the application. If the user is using a locale which uses dots instead of commas to format decimal values then dots will be used. If you want change the locale you should call Application.setLocale(…) with your preferred locale, in this case a european locale.

If you only want to change the locale for the table fields and not for the whole application you could replace the getLocale() call with a specific locale which uses the comma instead of a dot (here I use a finnish locale):

DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("fi"));

Edit: A small note, the pattern should be “#.00”. A comma in the pattern represents the grouping separator while the dot represents the decimal separator so you cannot just replace the dot with the comma. The locale specifies which kind of separator is used, not the pattern.

Thank for your help but this still not working…

		
Table table = new Table(){
		@Override
		protected String formatPropertyValue(Object rowId, Object colId,Property property) {
			                Object v = property.getValue();
			                if(v instanceof Double){
			                	DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("fi"));
			                	df.applyPattern("#.00");
			                	return df.format(v);
							}else if (v instanceof Date) {
			                	SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			                	return df.format(v);
			                }
			                return super.formatPropertyValue(rowId, colId, property);
			            }
			   };

This is the code to test in my app…Amount is a Double

The output on Column amount is for example : 153.0, 127.0,123.1 or 612.59985 (¿?)

I don’t know what is happening…because the field date format is ok…

Xavier


This seems to work
.

Finally works!!

Thanks!!

Hi:

I apply the format property value as you say and it works perfect (i’m doing it to format Double numbers), but after this I add a generated column, and the formatting doesn’t apply to it…how can I recall or somtheing the formatting?

regards, and thank you very much…

Hugo

Hello,

Your tip gave me a good solution to problems I have with European values in java.
But what have I do when user change the value? In this case I get the decimal point instead the decimal comma (see screenshot in Attachment)?
It’s the first time I use attachments in this tool. So I hope it works.
Have you got some good tips to me?
Walter
12473.doc (45 KB)