Sunday 5 June 2011

Decimal Rounding in an Apex

Hi,

As I was asked by my colleague how to round any decimal to certain decimal point, he was new to Salesforce. I looked in Math Methods in Apex Lang Ref. but could not find any as Round method there only returns floor or celling integer value removing all the decimal points. Then I suggested him to basic mathematic formula that my teacher told me in 10th standard
1)Multiply your number with nth power of 10 where n is the decimal point up to which we want to round it.
2) Use Math.Round then
3)Again divide it with n th power of 10

He was amazed that is that the way we do these small things in Apex, his comment made me to go again to Apex Lang Ref. Now I found a direct method in Decimal Method



Do this in System Logs :
Decimal decimalvalue = 23.55564543;
//rounds it up to 3 decimal places
d = d.setscale(3);
system.debug('decimalvalue : ' + d.scale());

Debug Result -> decimalvalue : 23.556

Decimal decimalvalue = 23.55534543;
//rounds it up to 3 decimal places
d = d.setscale();
system.debug('decimalvalue : ' + d.scale());

Debug Result -> decimalvalue : 23.555

So to my colleague and all please use this one and don't complicate things :)

Regards

27 comments:

  1. small but very useful, thanks for sharing this :)

    ReplyDelete
  2. Don't forget that Salesforce defaults the rounding mode to half-even which means 0.5 is rounded to 0 and 1.5 is rounded to 2.

    As an alternative, use,
    aNumber.setScale(aDecimals,System.RoundingMode.HALF_UP)

    ReplyDelete
  3. Thank you, chaps. Most useful that is.

    ReplyDelete
  4. I'd appreciate if you share some links to other sources that carry info on this subject if you are aware of some.

    ReplyDelete

  5. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.


    Salesforce training in Chennai

    ReplyDelete
  6. This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.

    digital marketing course in chennai
    hadoop training in chennai

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    AWS Training in Bangalore

    AWS Training in pune

    ReplyDelete




  9. Thanks For Sharing Your information Please keep updating us The Information Shared Is Very valuable Time Just Went On redaing The article Python Online Training Data Science Online Training Aws Online Training Hadoop Online Training

    ReplyDelete
  10. An amazing blog, it is very useful and Excellent Blog! I would like to say thanks for the efforts you have made in writing this post.



    ExcelR Data Science Course in Bangalore

    ReplyDelete
  11. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    date analytics certification training courses
    data science courses training
    data analytics certification courses in Bangalore
    ExcelR Data science courses in Bangalore

    ReplyDelete
  12. I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy.
    AI learning course malaysia

    ReplyDelete
  13. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.




    BIG DATA COURSE MALAYSIA

    ReplyDelete
  14. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page digital marketing course in singapore

    ReplyDelete
  15. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page digital marketing course in singapore

    ReplyDelete
  16. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page! digital marketing course in singapore

    ReplyDelete
  17. This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this,
    Data science Courses banglore

    ReplyDelete
  18. I have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation. Thanks a lot for keeping great stuff. I am very much thankful for this site.
    AI course malaysia

    ReplyDelete

  19. Excellent blog with lots of information, keep sharing. I am waiting for your more posts like this or related to any other informative topic.Amazing web journal I visit this blog it's extremely marvelous. Interestingly, in this blog content composed plainly and reasonable. The substance of data is educational

    Data Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course

    ReplyDelete
  20. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
    a href="https://www.excelr.com/data-analytics-certification-training-course-in-pune/"> Data Analytics Course in Pune/">Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…

    ReplyDelete
  21. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
    Data Analytics Course in PuneWow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…

    ReplyDelete
  22. Very nice article. I enjoyed reading your post. very nice share. I want to tweet this to my followers. Thanks !.
    data scientist training and placement in hyderabad

    ReplyDelete
  23. This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it,
    cyber security training malaysia

    ReplyDelete
  24. I expect this is an enlightening promote and it is totally helpful and educated. consequently, I would resulting to gratitude for the endeavors you have made recorded as a hard copy this article. thanks! YTD With Crack

    ReplyDelete
  25. Took me length to door all the explanation, but I actually partook in the article. It ended up being Very respecting me not entirely settled to all the analysts here! Its consistently accessible through now you can't unmarried-handedly be educated, yet with engaged! thanks! Quotes For Birthday

    ReplyDelete

Tweet