function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joe HayesJoe Hayes 

Decimal Multiplier Problem

This might be simple but I don't know why it doesnt work.
 
Decimal pricePerPerson = 100.00;
Decimal sellPerPerson = 1.1764705882352 * pricePerPerson;
sellPerPerson = sellPerPerson.setScale(2);
This returns 117.65 as expectd.

However this...
Integer discountPercent = 15;
Decimal pricePerPerson = 100.00;
Decimal sellPerPerson = (100/(100-discountPercent)) * pricePerPerson;
sellPerPerson = sellPerPerson.setScale(2);
returns 100.00 and I'm not sure why. It should also return 117.65
 
Best Answer chosen by Joe Hayes
Sai PraveenSai Praveen (Salesforce Developers) 
Hi joe,

As discountPercent is declared as Integer so (100/(100-discountPercent)) is returning Integer Value which is 1 and it is multipled with 100.00 gives you 100.00.

Instead of that define discountPercent as decimal then you will get required result.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

mukesh guptamukesh gupta
Hi Joe,

Please use below code :-
 
Decimal discountPercent = 15.00;
Decimal pricePerPerson = 100.00;
Decimal sellPerPerson = 100/(pricePerPerson-discountPercent) * pricePerPerson;
sellPerPerson = sellPerPerson.setScale(2);

system.debug('sellPerPerson== '+sellPerPerson);
 
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
mukesh guptamukesh gupta
Hi Joe,

Or you can use below:-  both of code is tested my side
 
Decimal discountPercent = 15.00;
Decimal pricePerPerson = 100.00;
Decimal sellPerPerson = 100/(100-discountPercent) * pricePerPerson;
sellPerPerson = sellPerPerson.setScale(2);

system.debug('sellPerPerson== '+sellPerPerson);

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi joe,

As discountPercent is declared as Integer so (100/(100-discountPercent)) is returning Integer Value which is 1 and it is multipled with 100.00 gives you 100.00.

Instead of that define discountPercent as decimal then you will get required result.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Joe HayesJoe Hayes
Setting discountPercent as decimal rather than Integer has worked. Thanks