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
Benjamin Zerbib 3Benjamin Zerbib 3 

Double returns number with decimal part

Hello everybody!

I have a short Apex Class that is supposed to paste a number in a Text field.
it's working but my issue is that it is pasting the number with a decimal value (example: '3.0' instead of '3').

How can I resolve this small issue, please?

Here is my code in case you need it:
 
public class CMC_Automation_Code {
    
    @InvocableMethod
    public static void updateAccount(List<String> AccountIds){
        Double CMC_Value = 0;
        Account con=[SELECT Id, CMC_Auto_Number__c FROM Account where Id in:AccountIds];
        List<Account> AccList = [SELECT Id, CMC_Auto_Number__c, CreatedDate FROM Account where CMC_Auto_Number__c != null
                                 order by CreatedDate desc limit 1];
        if(AccList != null && AccList.size()>0){
                CMC_Value = AccList[0].CMC_Auto_Number__c +1;
           }
        con.Global_Account_ID__c = 'CS-' + String.valueOf(CMC_Value);
        update con;
    }

}

Thanks a lot!

Benji​​​​​​​
Best Answer chosen by Benjamin Zerbib 3
ANUTEJANUTEJ (Salesforce Developers) 
Hi Benji,

As the CMC_Value is a Double value you are seeing even the decimal part.

try using the intValue();

For better understanding, you can check the below snippet of how it behaves.
 
Double val= 0;
Account a= [select Count__c from Account where id='0012w00000YnE0nAAF'];
val=a.count__c;
system.debug(val);
system.debug(string.valueOf(val));
system.debug(string.valueOf(val.intValue()));

count__c is a field on the account object.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Benji,

As the CMC_Value is a Double value you are seeing even the decimal part.

try using the intValue();

For better understanding, you can check the below snippet of how it behaves.
 
Double val= 0;
Account a= [select Count__c from Account where id='0012w00000YnE0nAAF'];
val=a.count__c;
system.debug(val);
system.debug(string.valueOf(val));
system.debug(string.valueOf(val.intValue()));

count__c is a field on the account object.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Benjamin Zerbib 3Benjamin Zerbib 3
Anutej thanks a lot!
It's working!