function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ToddKruseToddKruse 

Trying to display decimal as currency

I have VF page using a <table> with a <repeat> tag in it that is displaying information returned from an apex Class in the form of a <list> object.

 

The problem is, I am having to convert my decimal values to a string, format it with a dollar sign, put in the commas, etc.  Well, with the govenors in salesforce, this is getting taxing on the system.  Is there anyway to take a decimal and display it as currenty in the VF page?

 

 

 

So I am returning a list object,  and below is a sample of the <repeat> tag within the <table> tag.

 

<apex:repeat value="{!ReportValues}" var="RV">

                    <tr> 

 

                        <td width = "70px" align="right">

                            <apex:outputLabel value="{!RV.totApril}" />

                        </td>

                        <td width = "70px" align="right">

                            <apex:outputLabel value="{!RV.totMay}" />

                        </td>

 

    </tr>

</apex:repeat>

 

 

Thanks for any help you can give me.

 

--Todd Kruse

 

pmozz01pmozz01

If it is a SFDC currency field that you are trying to display, using outputField on your VF page will render the field as currency.  That much I have working, but am having problems with (1) aligning the decimal points in a pageBlockTable column and (2) converting a double to a currency string.  I have posted my code for (2) if anyone has suggestions.  The error I am getting is "Return value must be  of type Double.  Of course, I can eliminate that error if I  return rtn; but that does not display two decimal places.  

 

Any ideas appreciated!  Thanks.

private static string DecimaltoCurrency(Decimal val) { if (val==0) return '0.00'; string str = val.divide(1,2,RoundingMode.HALF_UP).format()+'.00'; integer dec = str.lastindexOf('.'); system.debug('-------DecimaltoCurrency: -str/dec/len------------->'+str + '/'+dec+'/'+str.length()); if (dec==-1 ) return str.substring(0,str.length()-2)+'.00'; //else if (dec==str.length()-1) return str+'0'; else return str.substring(0,dec+3); } public double getTotalAmount() { double rtn=0.00; system.debug('newItems:'+newItems); if (newItems!=null && newItems.size()>0) { for (LineItem li : newItems.values()) { if (li.item!=null) rtn+=(li.item.UnitPrice__c * li.item.Quantity__c); } } return DecimaltoCurrency(rtn); }

 


 

ToddKruseToddKruse

thanks for the reply.  But outputField won't work because I am returning a custom list that has variables.  If I change the variable from a string to a decimal, and set the outputLabel to outputField, it throws an error saying I can only use outputField with SObjects.

 

--Todd

 

 

pmozz01pmozz01

Todd - I was just able to get this to work and it is referring to a list of variables (line items) as well.

 

Hope it helps.  Now, if I could only get my decimal points to align.

private static string DecimaltoCurrency(Decimal val) { if (val==0) return '0.00'; string str = val.divide(1,2,RoundingMode.HALF_UP).format()+'.00'; integer dec = str.lastindexOf('.'); system.debug('-------DecimaltoCurrency: -str/dec/len------------->'+str + '/'+dec+'/'+str.length()); if (dec==-1 ) return str.substring(0,str.length()-2)+'.00'; //else if (dec==str.length()-1) return str+'0'; else return str.substring(0,dec+3); } public string getTotalAmount() { double rtn=0.00; system.debug('newItems:'+newItems); if (newItems!=null && newItems.size()>0) { for (LineItem li : newItems.values()) { if (li.item!=null) rtn+=(li.item.UnitPrice__c * li.item.Quantity__c); } } return DecimaltoCurrency(rtn); } private void renumItems() { map<integer,LineItem> items = new map<integer,LineItem>(); for (LineItem li : newItems.values()) { items.put(items.size(),li); } newItems=items; } ****************************************************** In Visualforce reference the variable: Invoice Total Amount : <h3>{!TotalAmount}</h3>