How to Convert Integer Data Type to Decimal in Salesforce Apex

While working on Apex in Salesforce, I created a field that stores the total price of products as an Integer representing the amount in rupees. To display this value to the user, I converted it into a decimal format representing dollars.

Here, I will explain why we need to convert integers to decimals. Then, using Apex code, we will see how to convert integer data types to decimals in Salesforce.

Why do we need to convert integers to decimals in Salesforce?

In Salesforce, sometimes we create integer data types instead of decimal or double data types. For example, the integer data type stores currency values, and when we convert the currency rate from one type to another, it returns a value in decimal form. However, the Integer data type cannot store decimal values.

Another example is that dividing any integer value by a number does not always give an absolute value. Additionally, displaying the result in an integer data type will result in an error. For that, we need to convert the integer data type to a decimal.

Convert Integer Data Type to Decimal in Salesforce Apex

I will explain here how to convert an integer data type to a decimal using Salesforce Apex.

In the apex code below, you can see I created an integer variable and stored a value in it. Then, as I multiplied the declared value by the decimal value, I got an error while storing the result in the integer data type.

For that, I will show you how to convert an integer to a decimal so that we can store a decimal value.

How to Convert Integer Data Type to Decimal in Salesforce Apex

Here, I converted an integer to a decimal using the Decimal. The valueOf() method also displayed the value in decimal form.

public class ConvertDataType {
    
    public void IntegerToDecimal(){
        
        Integer i = 840;
        Decimal d = Decimal.valueOf(i) * 81.12;
        System.debug('After changing currency rate the number is: ' +d.setScale(2));
    }
}  

Here, we created a method and developed logic to convert integers to decimals. First, I declared an integer variable to store a value of type INR. Now, we want to convert this into a dollar, and for that, we need to multiply by the dollar rate.

Then, as we declared the Decimal.valueOf(i) method and passed an integer variable to convert it into a decimal. After that, using the setScale(number) function, we can decide how many digits we have to display after the decimal point.

Method to convert Integer to Decimal Using Apex in Salesforce

In this way, we can convert integer data types into decimals using Apex in Salesforce.

Conclusion

I hope you have an idea about converting integers to decimals using Salesforce Apex. In this tutorial, I explained why we need to convert the integer data type into a decimal and which method we can use to do so.

Additionally, I explained another method for decimal values, allowing us to decide how many digits to leave after the decimal point.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.