+ Start a Discussion

Trying to return currency value - 2 decimals

Hi, I need to return a value so that it looks like currency ($1.00), but instead end up with 1.0.  Can anyone help?





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); } }




If this is inside a visual force page,you can format out put using the <apex:outputText> tag and specify the format. If this is not in visual force page, not sure what the exact use case is. 
Thanks, figured it out.