You need to sign in to do that
Don't have an account?

formatting numbers for display in Visualforce page - commas for thousands, etc.
If you are displaying a field from an sObject, <apex:outputField> does a nice job of adding decimal points and commas for thousands, etc.
If you are displaying your own variable (decimal), you cannot use <apex:outputField> becuase it is not part of an sObject.
Is there some easy way to add formatting to change 123456.78 into 123,456.78 dynamically? Do I need to convert the number to a string and then manipulate? Do I need to use JS to modify client-side? These would work but seem heavy-handed for what see be a common requirement.
If you are displaying your own variable (decimal), you cannot use <apex:outputField> becuase it is not part of an sObject.
Is there some easy way to add formatting to change 123456.78 into 123,456.78 dynamically? Do I need to convert the number to a string and then manipulate? Do I need to use JS to modify client-side? These would work but seem heavy-handed for what see be a common requirement.
Message Edited by TehNrd on 11-07-2008 04:20 PM
The new problem is that this method doesn't allow you to display (for example) exactly 2 decimal places. So you end up with a visualforce-displayed table of values that looks like this:
I wrote a function to do this, it's pretty lame but it works. I'm sure some genius out there could optimize it or come up with a better method.
public static String formatDouble(Double num, Integer decimalplaces) { Double multiplier = Math.pow(10, decimalplaces); num = num*multiplier; Long rounded = Math.roundToLong(num); num = rounded/multiplier; String formated = Decimal.valueOf(num).format(); //add zeros if needed Integer dotIndex = formated.indexOf('.'); if(dotIndex < 0) { formated += '.'; for(Integer i=0; i<decimalplaces; i++) { formated += '0'; } } else { Integer needed = decimalplaces - formated.substring(dotIndex+1).length(); for(Integer i=0; i <needed; i++) { formated += '0'; } } return formated; }