Split Expenses in Apex Code

We’ve all been there you go out for dinner with friends, someone picks up the bill, and then comes the tricky part: figuring out who owes how much. While you can always pull out a calculator, why not make this easier with a little Apex?

In this blog post, we’ll write a simple method that splits a bill evenly among a group of people. It’s the perfect example of basic math, condition handling, and return values in Apex.

If you’re just getting started with Salesforce development, this is a great way to practice writing clean and logical code with a real-world twist!

🧠 What Are We Trying to Do?

 

Let’s say your total bill is $200, and there are 4 people in the group. We want our method to:

  • Take the total bill amount

  • Take the number of people

  • Divide the bill equally

  • Return the amount each person should pay

We also want to make sure the number of people is greater than zero and the bill amount is not null because dividing by zero or using invalid inputs would break things!

🔍 Code Explanation

 

public with sharing class ApexUseCaseSeven {
    
    public static Decimal splitBill(Integer numberOfPeople, Decimal billAmount){

We define a class ApexUseCaseSeven and inside it, a static method named splitBill. It takes two inputs:

  • numberOfPeople: how many people are sharing the bill

  • billAmount: the total bill value

 
        Decimal splitPerPerson = 0;

We initialize a variable to store the result — the amount each person should pay.

        if(billAmount != null && numberOfPeople > 0){
            splitPerPerson = billAmount / numberOfPeople;
        }

Here’s the main logic:

  • We check if the bill amount is not null

  • We also ensure the number of people is greater than zero

  • If both conditions are met, we divide the total by the number of people

 
        return splitPerPerson;
    }
}

Finally, we return the result. If any input is invalid, it defaults to 0.

✅ Why This Is Useful

 

This simple method is great for:

  • Practicing input validation

  • Performing mathematical operations in Apex

  • Writing utility functions for everyday use cases

You could easily extend this method to:

  • Round the result to two decimal places

  • Add a tip percentage

  • Handle uneven splits or custom shares

It’s a small example, but it’s a good start for building problem-solving skills in Apex.

🧾 Final Code Snippet

 
public with sharing class ApexUseCaseSeven {   
    public static Decimal splitBill(Integer numberOfPeople, Decimal billAmount){
        Decimal splitPerPerson = 0;
        if(billAmount != null && numberOfPeople > 0){
            splitPerPerson = billAmount / numberOfPeople;
        }
        return splitPerPerson;
    }
}

🎥 Watch It in Action!

Want to see how this works in real-time? Watch this short video where I test the method and explain each step in the Developer Console.

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)