Skip to Content
author's profile photo Former Member
Former Member

Big decimal rounding


I have a bigdecimal numbers 5.5, 1.2, and 0.5. I want to convert all these numbers as 6, 2, 1.

Could you please tell how to achieve this?. I tried with ceiling, round_up, but I didnt get exact result.



Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Posted on Oct 18, 2006 at 12:00 AM

    Please try this.

    import java.math.*;
    public class TestBigD {
    	public static void main(final String[] args) {
    		BigDecimal bd = new BigDecimal("12.2");
    		BigInteger bi = new BigInteger("0");
    		bi = bd.setScale(0, BigDecimal.ROUND_CEILING).toBigInteger();


    Rich Heilman

    Add a comment
    10|10000 characters needed characters exceeded

    • Neither do I 😊

      In this case, BD is a BigDecimal which has a value of 12.2. I created an instance of BigInteger call BI, so we use the setScale method of BigDecimal to set how we want to round this value, notice the ROUND_CEILING, of course this will always round up regardless if it is less than 5 or greater, then it is passing the rounded value to our BigInteger bi. Then simply writing out bi.


      Rich Heilman

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.