Sub topic 4
Numbers, Currency and Percent: Precision and Scale
This chapter assumes you can create a custom field in Object Manager and know the difference between a standard and a custom field. It covers the three numeric field types and the two settings that control how they store values.
The three numeric types
All three types store decimal numbers. They differ in how the value is displayed and how it behaves in the rest of the platform.
| Type | Use it for | Notes |
|---|---|---|
| Number | Counts, scores, measurements | Plain number, no symbol |
| Currency | Money | Shows a currency symbol. In orgs with multiple currencies, values convert using exchange rates. |
| Percent | Rates and ratios | Shows a % sign |
Use Currency for money even when you only have one currency. It keeps formatting consistent and lets you turn on multiple currencies later.
Precision and scale
Two settings define the size of a numeric field:
- Precision is the total number of digits.
- Scale is how many of those digits come after the decimal point.
The maximum precision is 18. A field with precision 5 and scale 2 holds three digits before the decimal point and two after, so its largest value is 999.99.
In Setup you enter the number of digits to the left of the decimal point (Length) and the Decimal Places. Metadata stores the total as precision, so Length 3 and Decimal Places 2 become precision 5 and scale 2.
Reading precision and scale in Apex
You can check the settings of any numeric field with a describe call.
Schema.DescribeFieldResult amount = Opportunity.Amount.getDescribe();
System.debug(amount.getType()); // CURRENCY
System.debug(amount.getPrecision()); // 18
System.debug(amount.getScale()); // 2Common errors
- Choosing too few digits. A value with too many whole digits is rejected on save. Plan for the largest value you expect, not the typical one.
- Choosing too many decimal places. Salesforce rounds values with more decimals than the scale allows, so extra detail is lost. If you need exact results, set the scale high enough before data arrives.
- Treating Percent as a fraction. In the UI, Apex and SOQL, a Percent field showing 25% holds 25. In a formula, the same field is treated as 0.25. Check which one you are using when you mix them.
- Storing identifiers as Number. Phone numbers, postal codes and account numbers are not quantities. Leading zeros are dropped and you never calculate with them. Use a Text field instead.
- Forgetting that Number is not an integer type. A Number field with scale 0 holds whole numbers, but you must set the scale to 0 yourself.
Choosing between them
Ask what the value represents. Money means Currency. A ratio that people read as "x%" means Percent. Everything else you count or measure means Number. Then set the scale to the smallest number of decimals your reports need, and the length to your largest realistic value.
Exercise
You need a field for a discount rate that can reach 100% and is entered with one decimal place, such as 12.5%. Which type do you choose, and what length and decimal places do you set?
Show answer
Choose Percent. Set Length to 3 (to allow 100) and Decimal Places to 1, which is precision 4 and scale 1. This holds values from 0.0 to 100.0 and accepts 12.5.