Activity Forums Salesforce® Discussions How to round the Double to two decimal places In Salesforce Apex?

  • shariq

    Member
    September 19, 2018 at 7:43 am

    Hi,

    Use the below snippet of code :

    Decimal d = 100/3;

    Double ans = d.setScale(2);

    Thanks

  • Parul

    Member
    September 19, 2018 at 9:27 am

    You can also use this:

    Decimal toround = 3.14159265;
    Decimal rounded = toround.setScale(2);
    system.debug(rounded);

     

    thanks

  • Prachi

    Member
    September 19, 2018 at 1:21 pm

    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

  • Aman

    Member
    September 29, 2018 at 4:50 pm

    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.

Popular Salesforce Blogs