-1

I have an issue with Big decimal variable. I want to divide it, and the problem is that the deviation results is not correct. the variable called bvalue = 405000000 and I want to divide it by 1000000. the problem that I am facing is that the response is 1. and not 405 as expected. I used

bvalue = bvalue.divide(bvalue,1000000,RoundingMode.CEILING); 

how can I divide big decimal correctly? by 1000000 regards

enter image description here

enter image description here

2 Answers 2

2

You're calling the wrong .divide() function you wanted the following

BigDecimal bvalue = new BigDecimal(405000000);
BigDecimal avalue = new BigDecimal(1000000);
System.out.println(bvalue.divide(avalue, BigDecimal.ROUND_CEILING));
0
1

Your question looks almost like a typo to me, because you are dividing a number by itself:

bvalue = bvalue.divide(bvalue, 1000000, RoundingMode.CEILING);
         ^^^           ^^^  numerator = denominator

Hence the quotient of 1 is to be expected.

I think this is what you have in mind:

BigDecimal a = new BigDecimal("405000000");
BigDecimal b = new BigDecimal("1000000");
BigDecimal result = a.divide(b, 2, BigDecimal.ROUND_CEILING);
System.out.println(result);

Demo

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.