0

I have multiple currency/percent fields on which I need a method to add Zeros at the end. for example I have a currency(18,2) with value 4590, should end as 4590.00. percent(3,3) with value 3.3 should be 3.300. I need a reusable method which does this. Any help is much appreciated.Thanks!

2
  • Where are you wanting to do this? In Visualforce page? – Eric Nov 4 '15 at 1:08
  • @Eric : In apex for string concatenation – cartman Nov 4 '15 at 1:10
1

If you are talking about displaying it on a VF page you can use one of these:

<apex:outPutText value="{0,number,currency}">
   <apex:param value="{!YOURFIELD}"/>
</apex:outPutText>

or to control decimal places:

<apex:outPutText value="{0,number,$#,###.000}">
   <apex:param value="{!YOURFIELD}"/>
</apex:outPutText>

http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html

for a string in apex simply do this:

public static string formatInteger(Integer i, integer places){
   if(i == null) return '';
   return formatDecimal(decimal.valueOf(i));
}

public static string formatDecimal(Decimal d, Integer places){

   if(d == null || places == null) return '';

   d.setScale(places);
   return string.valueOf(d);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.