• #35227
    Forcetalks

    How to round the Double to two decimal places In Salesforce Apex?

    How to round the Double to two decimal places In Apex?

     

    #35228
    Salesforce | shariq shariq #1
    Forcetalks

    Hi,

    Use the below snippet of code :

    Decimal d = 100/3;

    Double ans = d.setScale(2);

    Thanks

    #35245
    Salesforce | Parul Parul #2
    Forcetalks

    You can also use this:

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

     

    thanks

    #35297
     Prachi #7
    Forcetalks

    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

    #40954
    Salesforce | Aman Aman #4
    Forcetalks

    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

Viewing 5 posts - 1 through 5 (of 5 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?