0

I have 4 Decimals fields with 2 digits, A, B, C and D. When I tried to multiply A*B and put them into C the C and D should be equals but they are not. What is the best practice to use the Decimal Fields?

Example:

We have A=3.34, B=4.97, C=16.60, D=16.59 and from the Validation Rule I tried to check if the C and D are equals but I did not have the case, but actually they should be equals.

3
  • 1
    what you have tried so far Commented Aug 9, 2017 at 20:04
  • I tried to use the ROUND function in the validation rule but it gave me this error that the C and D are not equals
    – BBS_
    Commented Aug 9, 2017 at 20:12
  • Best you list what you tried in the question in case someone can see a problem. But if the numbers are different in the last digit then you might have to resort to something like C > D - 0.01 && C < D + 0.01 to tolerate a small difference.
    – Keith C
    Commented Aug 9, 2017 at 20:36

1 Answer 1

2

I suggest you use the ROUND formula operator to control the rounding process and so the precision of the comparison if the problem is purely in non-Apex logic.

For Apex, fields are displayed in Visualforce UI with the number of decimal places specified in the SObject field definition. But they are stored in the database with their actual decimal places, so multiplying two decimals that each have 2 decimal places and storing the result in the database will result in a 4 decimal place number. So if it is Apex logic you are using, be careful to call setScale on decimal values that are the result of multiplication or division to move the result back to the same number of decimal places that are shown in the UI and so get consistent results.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .