+ Start a Discussion

Formating double as currency

The string.format() method listed in the Apex Documentation does not work.


This (and every other combinations I tried) generate a compile error 'method does not exist or wrong signature' !




    double subTotal = 1000.0;

    strind s = string.format('{0,number,currency}', subTotal);




Is there a way to do such a basic formating using APEX ?!?






I don't have an answer to your question, but it looks to me as if you have to supply a List of String as the second argument to String.format.



String s= string.format('{0} {1}', new List<String>{'3.14', '2.718'});



23:16:16 INFO - 20091010061616.289:AnonymousBlock: line 2, column 1: 3.14 2.718


The Apex guide says that string.format behaves like VisualForce outputText,  and the VF guide says to see this Java reference. But I can't get the number FormatType to work.




Message Edited by Shikibu on 10-09-2009 11:20 PM

I cannot get the format('{0,number,currency}) to work either.


I had to make a function that returns a properly formated currency string from a double...



I came across this once where I wanted to display a calculation that I did in my controller but couldn't format the currency correctly.  I solved this by creating a 'container', just an object that I don't commit to the database.  So I'd get my total then store it in my container and send that back to the page so I got the formatting for 'free'.  Try it out and let me know.
For an example of the hack that knichols is describing, see my post on formatting dated currencies in VisualForce.

Double to Currency Formatting



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