[SalesForce] Number Type, Decimal Points Confusion

We have a number typed field say Min_Distance__c where we have defined, Length=1 and Decimal places=2. But we experienced that salesforce allows to save numbers with more than 2 decimal places.

In pages, it shows the correct rounded values while queries or remote calls give the actual value(all the decimal places). Can someone confirm what is the behavior we should expect?

My thought is even in setup we define the decimal places, that is only for showing in visualforce pages. It saves whatever the decimal points it receives.

Best Answer

Only enforced in the UI (to maintain backward compatability). Apex can store up to 24 decimal places:

http://help.salesforce.com/HTViewSolution?id=000212661

Define a custom number field, say, "Number." Give it length = 3, and decimal places = 1. It might seem that this is done to restrict the precision of the field to two decimal places. However, on the UI level (on a standard edit page), if you try to type in, say, 237.631, it'll round off 237.631 when you click "Save" and store it in the database as 237.6 But if you then set a value via API, say 237.631, it'll be stored in the database - and returned via API (e.g. SOQL query) - as 237.631. In fact , SFDC will let you store up to 24 decimal points via API, regardless of the decimal places provided in the field definition. So you could save 237.123456789012345678901234.

This is completely working as designed and we kept the behavior so that it can be backward compatible.