How to Convert Decimal to Integer in Salesforce Apex

To convert a Decimal to an Integer in Salesforce Apex, use myDecimal.intValue(), which drops the decimal part (7.8 becomes 7). Integer.valueOf(myDecimal) compiles and gives the same result. To round instead of truncating, use myDecimal.round(System.RoundingMode.HALF_UP).intValue().

Several tutorials disagree on what Integer.valueOf() and type casting do with decimals, so I tested every method in the Developer Console of a Salesforce Developer Edition org. Below are the real results, including two errors you should know about and a rounding default that surprises most developers.

This Tutorial Covers: