62

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?

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
Jan Ajan
  • 1,461
  • 3
  • 12
  • 15

2 Answers2

99

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

Rohan Grover
  • 1,514
  • 2
  • 17
  • 23
0
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
  • 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. – c8999c 3f964f64 Apr 04 '22 at 14:00
  • 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](/help/how-to-answer). – Community Apr 04 '22 at 14:01
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31453667) – Clint Apr 06 '22 at 00:36