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
mpoiriermpoirier 

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' !

 

<code> 

 

    double subTotal = 1000.0;
   

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

 

</code> 

 

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

 

Thanks,

 

 

ShikibuShikibu

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

system.debug(s);

 

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
mpoiriermpoirier

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...

 

Thanks.

knicholsknichols
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.
ShikibuShikibu
For an example of the hack that knichols is describing, see my post on formatting dated currencies in VisualForce.
jarogyaswamyjarogyaswamy

Double to Currency Formatting

 

VisualForce

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