I have a field of type Percent(3,2) named Sector__c.VatRate__c. I initialized it with anonymous apex withe values like 5.5, 10, 20, etc. The code looks like that :
sec.VatRate__c = 10;
update sec;
Now I have a formula field on Opportunity named AmountWithTax__c, the formula is :
AmountWithoutTax__c * ( 1 + VatRate__c /100 )
I expected, for a record having AmountWithoutTax__c
set to 200, a result of 220, but I get 200.2.
Changing the formula to
AmountWithoutTax__c * ( 1 + VatRate__c )
gives me the expected result.
Trailhead states this :
A Percent is stored as a number divided by 100, meaning that 100% is represented as 1, 90% as 0.9, and so on.
So everything so far is ok.
But if I query the Sector object, the value returned for VatRate__c is 10, not 0.1. Worse, if I execute this code :
Sector__c[] sec = [Select VatRate__c from Sector__c where VatRate__c = 10];
Double d = 200.00;
System.debug(sec[0].VatRate__c);
System.debug(d * (1 + sec[0].VatRate__c) );
I get 10.00
and 2200
.
That looks not consistent to me, I expect an Apex code to return the same value as a matching formula...
Also the trailhead statement looks inaccurate, the percentage value seems to be stored as 90 for 90%, not as 0.9.
I tried to re-set the percentage value from the interface rather than from apex, but that changed nothing.
So am I missing something or is the trailhead doc wrong. And why does the formula return a number divided by 100 while Apex doesn't ?
Thanks