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
SoCal AdminSoCal Admin 

Does Charting support math? / Bug?

Anyone know how to get Charting to use math instead of concatenation?  The Chartclass is concatenating decimal variables instead of adding them.

 

I'm trying to create a stacked bar graph of 5 different products.  DeveloperConsole output shows the queried values are correct.

 

But when I try to SUM to values to create the Y Axis, it ends up like this: (see highlight)

 

from DeveloperConsole:

 

VCur: 3433.23

lmonth:    1222.32

lqtr:   454.34

lsales:  887.34

lprod: 232.23

vTotal:  3433.231222.32454.34887.34232.23

 

(this is a defined as a Decimal variable - how is this possible)?

 

I also tried casting each value - but no luck.

 

 

Here is the code:
 Decimal VCur;
        Decimal lmonth;
        Decimal lqtr;
        Decimal lsales;
        Decimal lprod;
     
        
        List<AggregateResult> qCur = [Select Sum(salesAmt__c) Sum from CTest__c where... ];
        List<AggregateResult> qmonth = [Select sum(salesAmt__c) Sum from CTest__c where ... ];
        List<AggregateResult> qqtr = [Select sum(salesAmt__c) Sum from CTest__c where ...];
        List<AggregateResult> qsales = [Select sum(salesAmt__c) Sum from CTest__c where ...];                
        List<AggregateResult> qprod = [Select sum(SalesAmt__c) Sum from CTest__c where...];                
       
        for (AggregateResult ar : Cur) {
            vCur = (Decimal)ar.get('Sum');
        }
                System.Debug('VCUR = ' + vCur);

        for (AggregateResult ar : qmonth) {
            lmonth = (Decimal)ar.get('Sum');
        }
                System.Debug('lmonth = ' + lmonth);

        for (AggregateResult ar : qqtr) {
            lqtr = (Decimal)ar.get('Sum');
        }
                System.Debug('lqtr = ' + lqtr);

        for (AggregateResult ar : qqtr) {
            lsales = (Decimal)ar.get('Sum');
        }
                System.Debug('lsales = ' + lsales);

        for (AggregateResult ar : qprod) {
            lprod = (Decimal)ar.get('Sum');
        }
                System.Debug('lprod = ' + lprod);

        Decimal vTotal = vCur + lmonth + lqtr + lsales + lprod;
        System.Debug('vTotal = ' + vCur + lmonth + lqtr + lsales + lprod);         

Best Answer chosen by Admin (Salesforce Developers) 
SoCal AdminSoCal Admin

Found the problem:

 

The System.Debug function converts the decimal parameters as STRINGs when listed in the function call. 

 

In my original casting attempts I was trying to solve the problem without a dedicated variable.  But I never replaced the parameters with the dedicated variable.

 

This is still non-intuitive, and should be castable.

 

This:   System.Debug(x + y + z), for Decimal reports this:  "123" instead of "6"

 

This:   int a = x + y + z

            System.Debug(a) reports the correct value of:  "6"

 

 

All Answers

SoCal AdminSoCal Admin

Found the problem:

 

The System.Debug function converts the decimal parameters as STRINGs when listed in the function call. 

 

In my original casting attempts I was trying to solve the problem without a dedicated variable.  But I never replaced the parameters with the dedicated variable.

 

This is still non-intuitive, and should be castable.

 

This:   System.Debug(x + y + z), for Decimal reports this:  "123" instead of "6"

 

This:   int a = x + y + z

            System.Debug(a) reports the correct value of:  "6"

 

 

This was selected as the best answer
Jay EcklesJay Eckles

System.Debug has only one signature, and that is System.Debug(String).  When you pass decimal arguments to it, it implicitly casts the arguments as strings.  If you pass Decimal + Decimal as an argument, the + is treated as a concatenation operator since the argument to the function is assumed to be a String.  It's the downside of operator overloading.