[java] Fix BigDecimal ROUND_HALF_EVEN

Jerry Quinn jlquinn@optonline.net
Sun Jun 1 20:20:00 GMT 2003


An IBM'er discovered the following problem in Classpath while trying
to benchmark a telco billing benchmark.

The following program should output 0.21 after scaling, but outputs
0.20.  The attached patch fixes the bug.  Is there a straightforward
way to add this to the test suite?

Jerry Quinn


import java.io.*;
import java.math.*;

public class jdec {
 public static void main(String args[]) {
     BigDecimal x = new BigDecimal("0.20562");
     System.out.println("before scale p " + x.toString());
     x=x.setScale(2, BigDecimal.ROUND_HALF_EVEN); // to x.xx
     System.out.println("after scale p " + x.toString());
 }}


06-01-2003  Jerry Quinn  <jlquinn@optonline.net>

	* java/math/BigDecimal (divide): Correctly handle
        ROUND_HALF_EVEN when amount is greater than 0.5.

*** BigDecimal.java.~1.9.~	Sat Apr 19 15:26:41 2003
--- BigDecimal.java	Sun Jun  1 13:14:41 2003
***************
*** 317,322 ****
--- 317,328 ----
  	  roundingMode = ROUND_DOWN;
  	else
  	  {
+ 	    // For example, .555
+ 	    if (parts[1].signum() != 0) {
+ 	      roundingMode = ROUND_UP;
+ 	      break;
+ 	    }
+ 
  	    int rightmost = 
  	      unrounded.mod (BigInteger.valueOf (10)).intValue ();
  	    if (rightmost % 2 == 1) // odd, then ROUND_HALF_UP



More information about the Java-patches mailing list