69

My code sample:

import java.math.*; public class x { public static void main(String[] args) { BigDecimal a = new BigDecimal("1"); BigDecimal b = new BigDecimal("3"); BigDecimal c = a.divide(b, BigDecimal.ROUND_HALF_UP); System.out.println(a+"/"+b+" = "+c); } } 

The result is: 1/3 = 0

What am I doing wrong?

4
  • 1
    Sorry about Lorem ipsum but it wouldn't allow me to post without it as "the question was too short". Commented May 17, 2012 at 14:06
  • Your result is correct. One-third, rounded to the nearest integer, breaking ties by rounding up, is indeed a flat, round zero. Commented May 17, 2012 at 14:06
  • So if i need 0.33333333? How do I have to divide 1 by 3? Commented May 17, 2012 at 14:08
  • 4
    Jan, you specify the scale as 8 for your case. a.divide(b,8, BigDecimal.ROUND_HALF_UP); Commented May 17, 2012 at 14:13

2 Answers 2

108

You haven't specified a scale for the result. Please try this

2019 Edit: Updated answer for JDK 13. Cause hopefully you've migrated off of JDK 1.5 by now.

import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { BigDecimal a = new BigDecimal("1"); BigDecimal b = new BigDecimal("3"); BigDecimal c = a.divide(b, 2, RoundingMode.HALF_UP); System.out.println(a + "/" + b + " = " + c); } } 

Please read JDK 13 documentation.

Old answer for JDK 1.5 :

import java.math.*; public class x { public static void main(String[] args) { BigDecimal a = new BigDecimal("1"); BigDecimal b = new BigDecimal("3"); BigDecimal c = a.divide(b,2, BigDecimal.ROUND_HALF_UP); System.out.println(a+"/"+b+" = "+c); } } 

this will give the result as 0.33. Please read the API

Sign up to request clarification or add additional context in comments.

2 Comments

Jan the link in my post is for 1.5 API . divide(BigDecimal divisor, int scale,int roundingMode) has been around for sometime now, I believe.
divide(BigDecimal divisor, int roundingMode) is @Deprecated(since="9")
-1
import java.math.*; class Main{ public static void main(String[] args) { // create 3 BigDecimal objects BigDecimal bg1, bg2, bg3; MathContext mc=new MathContext(10,RoundingMode.DOWN); bg1 = new BigDecimal("2.4",mc); bg2 = new BigDecimal("32301",mc); bg3 = bg1.divide(bg2,mc); // divide bg1 with bg2 String str = "Division result is " +bg3; // print bg3 value System.out.println( str ); } } giving wrong answer 

3 Comments

can you give an example what the script produces, and what it's supposed to produce instead? does it crash without an answer? Try to show what you have tried so far, and where you assume the error is.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.