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
John King20John King20 

How to compare two decimals

Hi,
So I have a trigger to share an amount among Contacts when when Account is updated. I'm trying to evaluate where if the "split" amount is 0.00 to show to 3 decimal places rather than 2, but I can't get the code
if (shareEachContact == 0.00) to evaluate.
Basically if 0.02 was to be split among 3 contacts I'd want it split 0.007, 0.007, 0.006.
I have functionality that works for 2 decimnal places but want to cater for 3 in the above scenarion. Code below.
trigger updateShareFromTotal on Account (after update) {
    List<Contact> childContacts = new List<Contact>();
    
    for(Account account : [SELECT Id, Total__c, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :Trigger.new]){
        if(account.Total__c != Trigger.oldMap.get(account.id).Total__c){
            Integer contactNumber = account.Contacts.size();
            Decimal shareEachContact = 0;
            shareEachContact = account.Total__c.divide(contactNumber,2);           
            if (shareEachContact == 0.00){
                shareEachContact = account.Total__c.divide(contactNumber,3);
            }     
            for (Integer i = 0; i < account.Contacts.size(); i++) {
                if (i == (contactNumber - 1)) {
                    account.Contacts[i].Share__c = account.Total__c - (shareEachContact * (contactNumber - 1));
                } else {
                    account.Contacts[i].Share__c = shareEachContact;
                }
                childContacts.add(account.Contacts[i]);
            }
        }
    }
    update childContacts;
}

 
mukesh guptamukesh gupta
Hi John,

Please use below code:-
 
trigger updateShareFromTotal on Account (after update) {
    List<Contact> childContacts = new List<Contact>();
    
    for(Account account : [SELECT Id, Total__c, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :Trigger.new]){
        if(account.Total__c != Trigger.oldMap.get(account.id).Total__c){
            Integer contactNumber = account.Contacts.size();
            Decimal shareEachContact = 0;
			Decimal convertTo3Decimail = 0.000;
            shareEachContact = account.Total__c.divide(contactNumber,2); 
            
			if(shareEachContact == 0.00){
				shareEachContact = convertTo3Decimail;
			}	
			
            if (shareEachContact == 0.000){
                shareEachContact = account.Total__c.divide(contactNumber,3);
            }     
            for (Integer i = 0; i < account.Contacts.size(); i++) {
                if (i == (contactNumber - 1)) {
                    account.Contacts[i].Share__c = account.Total__c - (shareEachContact * (contactNumber - 1));
                } else {
                    account.Contacts[i].Share__c = shareEachContact;
                }
                childContacts.add(account.Contacts[i]);
            }
        }
    }
    update childContacts;
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
John King20John King20
Thanks Mukesh but I can't appear to get this to work. It's still returning the share as 0.00. It's as if the "if(shareEachContact == 0.00)" isn't being evaluated as true, even though that's what's being returned. Is there an issue with comparing Decimals in this way?

Thanks
John King20John King20
So in this scenario
account.Total__c.divide(contactNumber,2);

if we have Total__c =0.02 and contact number = with the two decimal places, it's returning as 0.00 but the evaluation
if (shareEachContact == 0)

Isn't returning as true, am I misunderstanding something? Essentially I want a way to check if it's less than 0.01 so I can go to 3 decimal places. I'm aware this may not but the most efficient way. New to this​​​​​​.

Thanks for any help
John King20John King20
Please ignore, small issue with my code which was driving me crazy.