1

Question about percentage fields in Salesforce. I know salesforce is saving these values as double precision. However, it seems the value saved is a whole number > 1 instead of 0

For example:

//loanId is a percentage field with value 3%

loan__c l = [select interest_rate_L__c from loan__c where id = :loanId];
system.debug(l.interest_rate_L__c);     //3.000
integer n = 3;
system.debug(l.interest_rate_L__c * n); //9.000
system.debug(l.interest_rate_L__c + n); //6.000

Why does Salesforce treat percentage fields like full numbers? Is this the intended functionality? The documentation leads me to believe that it would save as .03:

Number: A positive or negative number, either integer or decimal.

Currency: A number that comes with a currency sign.

Percent: A number that comes with a percent sign, stored as a number divided by 100.

https://trailhead.salesforce.com/en/content/learn/modules/advanced_formulas/numbers_formulas

It doesn't seem to be a number divided by 100...

Is this the intended functionality? Do I have some setting off?

4
  • Why questions are notoriously difficult to answer, bordering on impossible for anyone who did not build the feature in question.
    – Adrian Larson
    Commented Dec 27, 2018 at 19:13
  • @AdrianLarson more trying to validate that this is actually the intended behavior since the documentation states that it is a number divided by 100 - I edited the question to more explicitly ask whether this is intended behavior. Thanks for the feedback.
    – ZAR
    Commented Dec 27, 2018 at 19:15
  • 1
    I also see the same behavior in Salesforce now, which was not happening previously. Was importing data from Excel into custom object with percent fields, value like 0.25 became 0.25% instead of 25%, and 0.035 became 0.04% instead of 3.5%. I had to multiply all percent fields values by 100 for the users to see the correct data in UI. Both selecting the fields in Workbench and outputting as debug statement in apex class results in 25 for 25%, not 0.25 and 3.5 for 3.5% not 0.035. Did Salesforce change something for Percent data type? Commented Jan 23, 2020 at 21:56
  • 1
    @NellyMorozova - sure seems like it -- I'm having the same issues Commented Jun 11, 2020 at 20:04

1 Answer 1

4

Salesforce saves the value as a fractional decimal (value/100), but this only helps you in formulas. If you want to use the value in Apex, Lightning, Visualforce, etc, you need to divide the percent field by 100.

7
  • 1
    So in my code example above, l.interest_rate_L__c + n should not equal 6.00, correct?
    – ZAR
    Commented Dec 27, 2018 at 19:25
  • 1
    @ZAR Assuming the percentage field is 0.03, then the result would be 3.03, as you'd expect. Make sure the data values are correct.
    – sfdcfox
    Commented Dec 27, 2018 at 19:46
  • 1
    @ZAR But, be aware if you store 3.0 in a percentage field, it will appear as 300% in the UI. Always make sure what data type you're working with.
    – sfdcfox
    Commented Dec 27, 2018 at 19:51
  • 2
    Coming back around on this. I just encountered the same problem and ran a test. I have a custom object with a field type of Percent -- I created a record and entered 1.1% as the value. In apex when I retrieve that value and System.debug the retrieved value it shows 1.1 .... if I then create a Decimal x = 1.1 and compare x to my retrieved value, Apex tells me that they are equal. Commented Jun 11, 2020 at 18:16
  • 1
    @captain_jim1 Not sure why I didn't see this comment before, but let me build an example. I'll be back.
    – sfdcfox
    Commented Jul 1, 2020 at 16:53

You must log in to answer this question.

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