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.

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.

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:
- Convert Integer Data Type to String in Salesforce Apex
- Convert a String to Decimal in Salesforce Apex
- Convert String to ID in Salesforce Apex
- Convert String to Datetime in Salesforce Apex
- Convert String to Blob in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.