Why Is My Currency Field Losing Decimal Precision?

When creating records through a Flow that calls an Apex class, you may encounter an issue where a currency field, such as Amount__c, does not retain its expected decimal precision. For example, when dividing two whole numbers like 500,000 / 12, you might expect to get 41,666.66, but Salesforce only shows 41,666.00. This problem occurs due to how Apex handles data types and arithmetic operations.

In the provided Apex class, both the Amount and SubscriptionTermMonth fields are declared as Integer. The critical line in the code looks like this:

projection.Amount__c = (Amount / SubscriptionTermMonth);

In Apex, which is based on Java, when both operands of a division are integers, the division operation itself is performed using integer arithmetic. That means the result will also be an integer, and any fractional part (the decimals) is discarded. Apex documentation confirms this behavior:

“If both operands are Integer or Long, integer arithmetic is performed. Only if at least one operand is a Double, the result will be a Double.”

Let’s see a simple example to illustrate this behavior:

Integer a = 10;
Integer b = 3;
System.debug(a / b); // Output: 3
System.debug((Double) a / b); // Output: 3.3333333333333335

In the first System.debug, both operands are integers, so Apex performs integer division and discards the decimal part. In the second example, since one operand (a) is explicitly cast to a Double, the division returns a floating-point (decimal) result.

To fix this issue in your Apex code, you just need to cast at least one of the operands to a Double. Update your problematic line as follows:

projection.Amount__c = ((Double) Amount / SubscriptionTermMonth);

By casting Amount to Double, the division now produces a decimal result, which maintains the correct precision when stored in your Currency (16,2) field in Salesforce.

If you prefer even more control over precision and rounding, especially when dealing with Decimal types, you can use the divide(divisor, scale) method. This method allows you to specify the number of decimal places (scale) to retain. For example:

Decimal total = Decimal.valueOf(500000);
Decimal months = Decimal.valueOf(12);
Decimal result = total.divide(months, 2);
System.debug(result); // Output: 41666.67

Using the Decimal class with its divide method ensures precise rounding behavior according to Salesforce’s standard rounding rules.

In summary, the loss of decimal precision occurs because Apex performs integer division when both operands are integers. To resolve this, ensure that at least one operand is a Double or Decimal, depending on your desired precision level. This simple casting fix will ensure your Flow-generated records correctly store values like $41,666.67 instead of $41,666.00.

Job-Oriented Salesforce Training with 100% Money Back Guarantee

Our Salesforce Training is designed to offer a complete understanding of the Salesforce platform, providing you with the necessary skills to excel in the CRM industry. The program covers essential modules such as Salesforce Admin, Developer, and AI, combining theoretical knowledge with practical experience. Through real-world projects and assignments, you’ll develop the expertise needed to solve complex business challenges using Salesforce solutions. Our expert instructors ensure you gain both technical skills and valuable industry insights to succeed in the Salesforce environment.

Along with technical expertise, our Salesforce Training in Bangalore offers personalized mentoring, certification preparation, and interview coaching to improve your career prospects. You’ll have access to comprehensive study resources, practical project experience, and continuous support throughout your learning journey. By the time you complete the course, you’ll be well-equipped for certification exams and capable of applying your problem-solving skills in real-world scenarios. Begin your Salesforce career with us and unlock limitless career opportunities. Enroll for a Free Demo today!

0
Would love your thoughts, please comment.x
()
x