Sub topic 5
Integer, Long, Double, and Decimal
This chapter assumes you can declare Apex variables, know that Apex is strongly typed, and can run anonymous Apex in the Developer Console. It covers the four numeric primitive types and the errors that come from choosing the wrong one.
The four numeric types
| Type | Size and precision | Typical use |
|---|---|---|
| Integer | 32-bit whole number, -2,147,483,648 to 2,147,483,647 | Counters, list indexes |
| Long | 64-bit whole number | Large counts, timestamps in milliseconds |
| Double | 64-bit floating point | Scientific or approximate values |
| Decimal | Arbitrary precision, with a scale (digits after the point) | Currency and exact values |
Salesforce Number and Currency fields map to Decimal. Whole-number fields can be read into Integer variables.
Integer count = 5;
Long population = 8000000000L;
Double ratio = 0.5;
Decimal price = 19.99;
System.debug(count + ' ' + population + ' ' + ratio + ' ' + price);A literal beyond the Integer range needs the L suffix, as in 8000000000L. Without it, the compiler rejects the literal.
Why Decimal for money
Double stores binary fractions, so many decimal values are approximate. Decimal stores base-10 digits exactly. Use Decimal wherever rounding errors would matter.
Use setScale to fix the number of decimal places and pick a rounding mode.
Decimal total = 10.00;
Decimal rate = 0.075;
Decimal tax = (total * rate).setScale(2, RoundingMode.HALF_UP);
System.debug(tax);The raw product is 0.75000. setScale(2, RoundingMode.HALF_UP) returns 0.75. Without a rounding mode, setScale throws an exception if it would need to discard non-zero digits.
Integer division
Dividing two Integers gives a result you may not expect. Convert one operand to Decimal when you need the fractional part.
Integer a = 7;
Integer b = 2;
System.debug(a / b);
System.debug(Decimal.valueOf(a) / b);The first line prints 3.5 in Apex, because / on Integers returns a Decimal result rather than truncating. If you need a whole number, convert it explicitly with Integer.valueOf(...) or .intValue(). This differs from Java and C, where Integer division truncates.
Widening and overflow
Apex widens numbers implicitly in this order: Integer, Long, Double, Decimal. A smaller type can be assigned to a larger one without a cast.
Integer small = 7;
Long widened = small;
Decimal asDecimal = widened;
System.debug(asDecimal);Going the other way needs an explicit conversion such as intValue(), and it may lose data. Integer arithmetic that exceeds 2,147,483,647 overflows silently. Declare the variable as Long and use Long arithmetic if values may grow that large.
Integer big = 2147483647;
Long safe = (Long) big + 1;
System.debug(safe);Common errors
- Using Double for prices, then comparing totals with
==and getting unexpected results. - Forgetting the
Lsuffix on a large literal. - Calling
setScalewithout a rounding mode on a value with extra digits. - Reading a Decimal field into an Integer variable without converting it first.
Exercise
You compute a sales tax of 8.25% on an order total of 49.99 and need the result rounded to two decimal places, rounding half up. Which type should the variables use, and which method call finishes the calculation?
Show answer
Use Decimal for the total, the rate, and the result, because Decimal is exact. Finish with .setScale(2, RoundingMode.HALF_UP), for example Decimal tax = (49.99 * 0.0825).setScale(2, RoundingMode.HALF_UP);.