cancel
Showing results for 
Search instead for 
Did you mean: 

Big decimal rounding

Former Member
0 Kudos

Hi,

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.

Thanks-in-advance.

Sunita.

Accepted Solutions (0)

Answers (1)

Answers (1)

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

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();
		System.out.println(bi);
	}

}

Regards,

Rich Heilman

Former Member
0 Kudos

Hi Rich,

I dont have much Java experience. Could you please explain a bit what it is doing in rounding.

Thanks,

Sunita.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

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.

Regards,

Rich Heilman