+ Start a Discussion
Ken_KoellnerKen_Koellner 

Convert decimal to string, is any format control available?

I want to convert a decimal to a string and force a certain format,  someting analogous to "##0.00" with the DecimalFormat class in Java.

 

If I use the format method, there's no control of the form.  When the value is 0, I get "0" whereas I want "0.00".

 

 

Is there any formatting facilty in Apex?

 

This is for going input a VF page but it's not an SObject field so I can use OuputField.

 

Best Answer chosen by Admin (Salesforce Developers) 
Kirk F.ax361Kirk F.ax361

A quickfix and a real answer.

 

Quickfix: Try the following:

 

<apex:outputText value="{0,number,0.00}">
     <apex:param value="{!thisDecimal}" />
</apex:outputText>

 

WARNING: this assumes your viewers all prefer the US-centric representation of

"1,234,567.89".  Other countries use other characters for thousands and decimals; for example, this same number should appear as "1 234 567,89" in parts of Europe. 

 

Real Answers:

 

http://community.salesforce.com/sforce/board/messa ge?board.id=Visualforce&thread.id=2620
Formatting Currency fields in a multi-currency org

 

http://community.salesforce.com/sforce/board/messa ge?board.id=Visualforce&thread.id=894
Convert a double field into a currency field in Visualforce

 

 

 

I use the quickfix method all the time, however.  For example, say I want to render a list of items, quantities, and prices.  Quantities (at least the stuff we sell!) are always whole, and rarely go over 99.  Showing the quantity as "1.00" is silly, so the following visualforce code cleans that up to "1", and only shows the decimals if they really actually do buy a portion of something:

 

<apex:outputText value="{0,number,#.##}">
     <apex:param value="{!thisDecimal}" />
</apex:outputText>