I have a Managed field Number(16, 2) of which number of decimal places cannot be edited.

For Example, if I enter 1.2391276549 into that field, It is displayed as 1.24 (as we know that salesforce internally saves all the decimal places and rounds it while displaying).

Question: Is there a way to get full number with all the decimal places (1.2391276549 instead of 1.24) via APEX and Formula?

  • Please note that I need a way to fetch this full decimal by doing a SOQL or Formula field from Apex code. – Divya Jan 4 '19 at 10:53

The length and decimal places are only enforced when editing data via the standard web UI. (i.e., Custom object | New field | Data type: Number | Check the fields - length and decimal places)

Apex and API methods can actually save records with decimal places. This is true for standard and custom fields. Salesforce changes the display to match the definition, but they are stored in the database as inserted.

When the user sets the precision in custom fields in the Salesforce application, it displays the precision set by the user, even if the user enters a more precise value than defined for those fields. However, when you set the precision in custom fields using the API, no rounding occurs when the user retrieves the number field.

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 Salesforce kept the behavior so that it can be backward compatible.

Reference:- Data type number field does not store more numbers in decimal places than defined

Also, Introducing SOAP API

  • Thank you for reply @sanket When you say "no rounding occurs when the user retrieves the number field". How do we retrieve without rounding? I tried this: system.debug([Select field_namefrom Account limit 1].field_name); – Divya Jan 4 '19 at 8:41
  • It still gives 1.24 when value entered was 1.2391276549 – Divya Jan 4 '19 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.