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
snolbu2snolbu2 

Sum function can't display decimal space

Hi all
I have a problem with SUM (aggregate function).

I summed currency Field(16, 2).
I expected 2 decimal spaces, but The result was just Long.
It's been occuring every currency field in Developer Console, Apex Code, Salesforce.schema
How Can I fix it ?

1. Field : M1_AR__c (Currency 16,2)
2. Query & Apex Code
   AggregateResult[] ar = Database.query(' SELECT Sum(M1_AR__c) , Sum(M1_Fixed__c) '+
                                                                            ' FROM KR_Invoice_New__c WHERE Batch_Month__c = \'2014-05\' '+
                                                                            ' AND M1_Arrear__c > 180 AND M1_Arrear__c < 361 '+
                                                                            ' AND Business_Closure__c <> \'closed business & Collection agency\' ');
   System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ' + ar[0].get('expr0'));
   System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ' + String.valueOf(ar[0].get('expr0')));
   System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ' + Decimal.valueOf(String.valueOf(ar[0].get('expr0'))));

3. Return Value
  - Expected Value :      1788272509.32
  - Real Return Value : 1788272509

4. Each Currency Value
  - Each Currency Value is stored with decimal space 2
  - They was summed => 1788272509.32
User-added image
Boom B OpFocusBoom B OpFocus
Have you tried using "(Decimal)ar[0].get('expr0')" instead of "ar[0].get('expr0')"?
snolbu2snolbu2
I know that Decimal is not supported on aggregate result.
But I tried and compile failed.

Basically, SOQL Sum display Number without decimal space