You need to sign in to do that
Don't have an account?

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
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
The original code above will show the user...
The code below just adds one more parameter to the opportunity. It will show the user...
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
page
controller code
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:
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
The original code above will show the user...
The code below just adds one more parameter to the opportunity. It will show the user...
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)
Something to try?
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; } }