LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Predict the Output · Question 15

BigDecimal arithmetic and scale Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 15 Back to Exercise

What is the output of the following Java program?

Main.java
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("10.25"), b = new BigDecimal("3");
        System.out.println(a.add(b) + " " + a.multiply(b) + " " + a.divide(b, 3, RoundingMode.HALF_UP) + " " + a.setScale(1, RoundingMode.HALF_EVEN) + " " + new BigDecimal("2.50").stripTrailingZeros());
    }
}
  • A 13.25 30.75 3.417 10.2 2.5 Correct
  • B 13.25 30.75 3.416 10.3 2.50
  • C 13.25 30.7500 3.417 10.3 2.5
  • D 13.25 30.75 3.417 10.3 2.5
Correct Answer: Option A

HALF_EVEN ("banker's rounding") rounds 10.25 to the even neighbour, 10.2. The scale of a product is the sum of the scales (2 + 0).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links