Data type for currency field in Apex in Salesforce

Data type for currency field in Apex in Salesforce

The data type for currency field in Apex should be “Double” or “Decimal“.

Currency may contain fractional portions (decimal place), so we have to use Double data type while we do some manipulations using currency field.

Check the following code for reference. Amount is standard Currency field in Salesforce Opportunity object.

Sample Code:

Opportunity objOppty = new Opportunity(
    Name = 'Testing',
    StageName = 'Prospecting',
    Amount = 900.78
);
//Integer is not supported for Currency field
//Integer intAmount = objOppty.Amount;
Decimal opptyAmountInDecimal = objOppty.Amount;
Double opptyAmountInDouble = objOppty.Amount;
System.debug( 'Decimal Value is ' + opptyAmountInDecimal );
System.debug( 'Double Value is ' + opptyAmountInDouble );

Output:

Leave a Reply