How to Convert Decimal to Integer in Salesforce Apex

In our Salesforce org, each product order has a sales tax calculated as a decimal. The finance department requires that the sales tax be reported as an integer (i.e., rounded to the nearest whole number) for better reporting and accounting.

For this requirement, we had to convert the values of calculated sales tax from Decimal to Integer. In this tutorial, I will explain the possible ways to convert decimals to integers in Salesforce Apex.

Convert Decimal to Integer using Integer.valueOf() method in Salesforce Apex

In Salesforce Apex, we can use the Integer.valueOf() method to convert the decimal to an Integer. This method takes the whole number part and excludes the number after the decimal.

Open the Salesforce Developer Console and execute the code below in the Apex Anonymous window.

Decimal decimalValue = 45.67;
Integer integerValue = Integer.valueOf(decimalValue);
System.debug ('Integer values is: ' + integerValue); 

Now, in the execution log window, we will see the decimal value converted to an integer in the debug.

Convert decimal to integer in Salesforce apex

This way, we can convert a decimal value to an integer in Salesforce Apex using the Integer.valueOf() method.

Convert Decimal to Integer using .intValue() method in Salesforce Apex 

In Salesforce Apex, we can also use the decimalvariable.intValue() to convert the decimal values into integers.

Now, execute the code below in the Apex anonymous window.

Decimal mydecval = 15.0;
Integer myintval = mydecval.intValue();  
System.debug('Integer value is: ' + myintval);

In the above code, we don’t need to pass any argument to the intValue().

Now, in the execution log window, we can see that the debug output returned the value as an integer converted from a decimal value.

Salesforce Apex convert decimal to integer

In this way, we can convert a decimal value to an integer in Salesforce Apex using the intValue() method.

Convert Decimal to Integer using Math.round() method in Salesforce Apex

In Salesforce Apex, the Math.round() method rounds the decimal value to the nearest Integer(whole number).

For example, it will round the decimal value 25.70 to 26, which means if the decimal value is less than 0.5, it will round up, and if it is more than 0.5, it will round up the integer value.

Now, to convert a decimal to an integer using the Math.round() method, execute the code below in the Apex anonymous window.

Decimal decimalValue = 45.67;
Integer integerValue = (Integer) Math.round(decimalValue);
System.debug('converted decimal to integer: ' + integerValue); 

Convert Decimal to Integer using Math.ceil() method in Salesforce Apex

To convert a decimal to an Integer, we can also use the Math.ceil() method. This method is similar to the Math.random() method, but unlike Math. random, it rounds a decimal number up to the nearest integer, regardless of the decimal part.

For example, if a decimal value is 15.70 or 15.30, it will round up to 16 in both cases.

Now, execute the code below in the Apex anonymous window to convert the decimal value to an integer.

Decimal decimalValue = 45.67;
Integer ceilValue = (Integer) Math.ceil(decimalValue); 
System.debug('the integer value is: ' + ceilValue);

Decimal decimalValue2 = 45.01;
Integer ceilValue2 = (Integer) Math.ceil(decimalValue2); 
System.debug('the integer value is: ' + ceilValue2);

In the above code, I have entered two decimal values greater and less than 0.5 to show that the Math.ceil() method rounds up both the values in the debug.

After executing the code, you will see in the debug log that both entered decimal values are rounded up to the integer value 46.

Round up integer values in Salesforce apex

This way, we can convert decimals to integers as rounded-up values in Salesforce Apex using the Math.ceil() method.

Convert Decimal to Integer using Math.floor() method in Salesforce Apex

In Salesforce Apex, the Math.floor() method is the opposite of the Math.ceil() method. It rounds a decimal number down to the nearest integer, excluding the decimal part. It doesn’t matter if the decimal part is greater than or less than 0.5; it always rounds down.

Now, execute the code below in the Apex anonymous window to convert the decimal value to an integer using the Math.ceil() method.

Decimal decimalValue = 36.75;
Integer floorValue = (Integer) Math.floor(decimalValue); 
System.debug('the integer value is: ' + floorValue);

Decimal decimalValue2 = 36.20;
Integer floorValue2 = (Integer) Math.floor(decimalValue2); 
System.debug('the integer value is: ' + floorValue2);

In the execution log window, we can see that the values 36.75 and 36.20 are rounded down to the integer value 36.

Salesforce Apex convert decimal to integer value with Math

This way, we can convert a decimal value to an integer with a round-down value in Salesforce Apex using the Math.floor() method.

Conclusion

In this Salesforce tutorial, we have learned to convert a decimal data type to an integer using several methods. We used integer methods such as Integer.valueOf and .intvalue() to convert a decimal value to an integer. After this, we used Math methods such as Math.round(), Math.cell(), and Math.floor() to round up the decimal values and convert them to an integer.

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.