Question 15
Back to Exercise
What is the output of the following Java program?
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!