Activity › Forums › Salesforce® Discussions › How to round the Double to two decimal places In Salesforce Apex?
Tagged: Code Snippet, Decimal, Double, Salesforce Apex, Salesforce Apex Code, Salesforce Help and Training
-
How to round the Double to two decimal places In Salesforce Apex?
Posted by Anurag algoworks on September 19, 2018 at 7:42 AMHow to round the Double to two decimal places In Apex?
Aman replied 7 years, 7 months ago 5 Members · 4 Replies -
4 Replies
-
Hi,
Use the below snippet of code :
Decimal d = 100/3;
Double ans = d.setScale(2);
Thanks
- [adinserter block='9']
-
You can also use this:
Decimal toround = 3.14159265;
Decimal rounded = toround.setScale(2);
system.debug(rounded);thanks
-
hi,
Decimal toround = 3.14159265;
Decimal rounded = toround.setScale(2);
system.debug(rounded);Or
What’s nice about this one is you can use the setScale overload that lets you specify the rounding mode you want to use.
Decimal toround = 3.14159265;
Decimal rounded = toRound.setScale(2, RoundingMode.HALF_UP);
system.debug(rounded);
In this case – I think you’ll want the HALF_UP rounding mode. It’s the traditional round to nearest, but round .5 up.Thanks
-
Hi,
Please Use the below snippet of code :
Decimal parentlistPrice = 9995.00
Decimal svcPercent = .17
Decimal discount = .88
//oli.quantity = 2.0
oli.TotalPrice = (((parentListPrice * svcPercent) * discount) * oli.Quantity).setScale(2);
Thanks
Log In to reply.