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
Drew1815Drew1815 

Convert a double field into a currency field in Visualforce

I was wondering how I can use a custom controller with a class variable, totalAmount, defined as a Double data type and have it displayed as a Currency field in a Visualforce Page? I know if I use a standard controller and use component that Visualforce will automatically render it as a currency field. But how do I accomplish that with a custom controller and a class field defined as a "Double" data type?

To elaborate, the scenario is to display the total amount of multiple line items. The visualforce page allows users to enter multiple lines items with varying amounts and at the bottom there is the "Total" amount. The VF/Apex code is properly aggregate the amount, but it is stored as a Double data type field in the Apex Code controller class - and therefore displayed in the page as a double.


Thanks.

Message Edited by Drew1815 on 04-14-2008 11:33 AM
Best Answer chosen by Admin (Salesforce Developers) 
Kirk F.ax361Kirk F.ax361
If you use multiple currencies, however, you might try a small addition to Ron's great code above.

The original code above will show the user...
  • the amount formatted in the currency style of the locale set in the current user's preferences
  • the 3-letter currency ISO code from the current user's preferences. This may not be the actual currency you want to show.

The code below just adds one more parameter to the opportunity.  It will show the user...
  • the amount formatted in the currency style of the locale set in the current user's preferences
  • the 3-letter currency ISO code you set from the object itself. (more accurate!)
  • if the user's preferred currency does not match the currency from the object, a courtesy conversion in (parentheses) to their preferred currency, derived from your org's "company profile | manage currencies" settings.
Updated Code:
public class thetest {
double d = 1234.5;
string theCurrencyIsoCode = 'USD';

public opportunity getopp() {
opportunity ret = new opportunity(amount=d, currencyIsoCode = theCurrencyIsoCode );
return ret;
}
}

In other words, instead of a French colleague who prefers Euros seeing
EUR 1 234,50

They would see
USD 1 234,50 (EUR 969.22)

All Answers

Ron HessRon Hess
here is my suggestion, it may appear clumsy, but i'm using the platform to format the double into a currency field on the page, the opportunity that is used (to format the field) is never saved to the db.

page

Code:
<apex:page controller="thetest" >
<apex:outputField value="{!opp.amount}" ></apex:outputField>
</apex:page>

 


controller code
Code:
public class thetest {
double d = 23.9;
public opportunity getopp() {
opportunity ret = new opportunity(amount=d);
return ret;
}
}

 


prbprb

This technique produces an eror if the field is defined as currency in an opportunity:

<apex:outputField value="{!cx.ProServ_Daily_Rate__c}" ></apex:outputField>

Produces:

Error: Currency fields on entities with effective dated currency are not supported.

How are you supposed to format currency fields.
The value for {!cx.ProServ_Daily_Rate__c} is "100.0" for example which looks wrong.

I want it to be 100.00

 

Kirk F.ax361Kirk F.ax361
If you use multiple currencies, however, you might try a small addition to Ron's great code above.

The original code above will show the user...
  • the amount formatted in the currency style of the locale set in the current user's preferences
  • the 3-letter currency ISO code from the current user's preferences. This may not be the actual currency you want to show.

The code below just adds one more parameter to the opportunity.  It will show the user...
  • the amount formatted in the currency style of the locale set in the current user's preferences
  • the 3-letter currency ISO code you set from the object itself. (more accurate!)
  • if the user's preferred currency does not match the currency from the object, a courtesy conversion in (parentheses) to their preferred currency, derived from your org's "company profile | manage currencies" settings.
Updated Code:
public class thetest {
double d = 1234.5;
string theCurrencyIsoCode = 'USD';

public opportunity getopp() {
opportunity ret = new opportunity(amount=d, currencyIsoCode = theCurrencyIsoCode );
return ret;
}
}

In other words, instead of a French colleague who prefers Euros seeing
EUR 1 234,50

They would see
USD 1 234,50 (EUR 969.22)
This was selected as the best answer
Kirk F.ax361Kirk F.ax361
Prb, I may have had the same problem as you: It disappeared when I remembered to always select the CurrencyIsoCode from the objects I wanted to render.  If I just selected the currency field, without selecting the CurrencyIsoCode, the currency would not render as I expected.

Something to try?
AFawcett (CODA)AFawcett (CODA)

I've wrapped Ron's suggestion in a handy, VF Component.

 

That formats Number and Date fields via <apex:outputField>.

 

Example Usage:

 

 

<apex:page standardController="MyObject__c"> <p> The formatted date is <c:formatter date="{!TODAY()}"/> </p> <p> The formatted value is <c:formatter number="{!myobject__c.MyField__c}"/> </p> </apex:page>

 

 

 

Source:

 

Note: VF Component attriubtes don't support Decimal data type currently, hence the conversion.

 

You'll also need a custom (or approprite standard) to act as the transient object to bind the outputField's to.

 

 

<apex:component controller="FormatterController"> <apex:attribute name="date" type="Date" description="A date value" assignTo="{!date}"/> <apex:attribute name="number" type="String" description="A number value" assignTo="{!number}"/> <apex:outputField value="{!formatted.Date__c}" rendered="{!formatted.Date__c!=null}"/> <apex:outputField value="{!formatted.Number__c}" rendered="{!formatted.Number__c!=null}"/> </apex:component>

 

public class FormatterController { private Date m_date; private String m_number; public void setDate(Date dateValue) { m_date = dateValue; } public Date getDate() { return m_date; } public void setNumber(String numberValue) { m_number = numberValue; } public String getNumber() { return m_number; } public Formatter__c getFormatted() { Formatter__c formatter = new Formatter__c(); formatter.Date__c = m_date; if(m_number!=null) formatter.Number__c = Decimal.valueOf(m_number); return formatter; } }