Java Code Examples for java.math.RoundingMode#HALF_DOWN

The following examples show how to use java.math.RoundingMode#HALF_DOWN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * divide(BigDecimal, MathContext)
 */
public void testDivideBigDecimalScaleMathContextHALF_DOWN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    int precision = 21;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mc = new MathContext(precision, rm);
    String c = "2.77923185514690367475E+26";
    int resScale = -6;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result = aNumber.divide(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", resScale, result.scale());
}
 
Example 2
Source File: TestResource.java    From AVM with MIT License 6 votes vote down vote up
@Callable
public static boolean testShadowJDKEnum(){
    boolean ret = true;
    ret = ret && (RoundingMode.HALF_UP == RoundingMode.valueOf("HALF_UP"));
    ret = ret && (RoundingMode.CEILING instanceof Object);

    RoundingMode[] es = (RoundingMode[]) RoundingMode.values();
    ret = ret && (es[0] == RoundingMode.UP);
    ret = ret && (es[1] == RoundingMode.DOWN);
    ret = ret && (es[2] == RoundingMode.CEILING);
    ret = ret && (es[3] == RoundingMode.FLOOR);
    ret = ret && (es[4] == RoundingMode.HALF_UP);
    ret = ret && (es[5] == RoundingMode.HALF_DOWN);
    ret = ret && (es[6] == RoundingMode.HALF_EVEN);
    ret = ret && (es[7] == RoundingMode.UNNECESSARY);

    return ret;
}
 
Example 3
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * remainder(BigDecimal, MathContext)
 */
public void testRemainderMathContextHALF_DOWN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = -45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 10;
    int precision = 75;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mc = new MathContext(precision, rm);
    String res = "1149310942946292909508821656680979993738625937.2065885780";
    int resScale = 10;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result = aNumber.remainder(bNumber, mc);
    assertEquals("incorrect quotient value", res, result.toString());
    assertEquals("incorrect quotient scale", resScale, result.scale());
}
 
Example 4
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Multiply two numbers of positive scales using MathContext
 */
public void testMultiplyMathContextScalePosPos() {
    String a = "97665696756578755423325476545428779810457634781384756794987";
    int aScale = -25;
    String b = "87656965586786097685674786576598865";
    int bScale = 10;
    String c = "8.561078619600910561431314228543672720908E+108";
    int cScale = -69;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(40, RoundingMode.HALF_DOWN);
    BigDecimal result = aNumber.multiply(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", cScale, result.scale());
}
 
Example 5
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Subtract two numbers of different scales using MathContext;
 *  the first is negative
 */
public void testSubtractMathContextDiffScaleNegPos() {
    String a = "986798656676789766678767876078779810457634781384756794987";
    int aScale = -15;
    String b = "747233429293018787918347987234564568";
    int bScale = 40;
    String c = "9.867986566767897666787678760787798104576347813847567949870000000000000E+71";
    int cScale = -2;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(70, RoundingMode.HALF_DOWN);
    BigDecimal result = aNumber.subtract(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", cScale, result.scale());
}
 
Example 6
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * round(BigDecimal, MathContext)
 */
public void testRoundMathContextHALF_DOWN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = -45;
    int precision = 75;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mc = new MathContext(precision, rm);
    String res = "3.736186567876876578956958765675671119238118911893939591735E+102";
    int resScale = -45;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal result = aNumber.round(mc);
    assertEquals("incorrect quotient value", res, result.toString());
    assertEquals("incorrect quotient scale", resScale, result.scale());
}
 
Example 7
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * divide(BigDecimal, scale, RoundingMode)
 */
public void testDivideBigDecimalScaleRoundingModeHALF_DOWN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 5;
    String b = "74723342238476237823787879183470";
    int bScale = 15;
    int newScale = 7;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    String c = "500002603731642864013619132621009722.1803810";
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result = aNumber.divide(bNumber, newScale, rm);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", newScale, result.scale());
}
 
Example 8
Source File: BigDecimalTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMathContextConstruction() {
       String a = "-12380945E+61";
       BigDecimal aNumber = new BigDecimal(a);
       int precision = 6;
       RoundingMode rm = RoundingMode.HALF_DOWN;
       MathContext mcIntRm = new MathContext(precision, rm);
       MathContext mcStr = new MathContext("precision=6 roundingMode=HALF_DOWN");
       MathContext mcInt = new MathContext(precision);
       BigDecimal res = aNumber.abs(mcInt);
       assertEquals("MathContext Constructer with int precision failed",
               res, 
               new BigDecimal("1.23809E+68"));
       
       assertEquals("Equal MathContexts are not Equal ",
               mcIntRm,
               mcStr);
       
       assertEquals("Different MathContext are reported as Equal ",
       		mcInt.equals(mcStr),
               false);
       
       assertEquals("Equal MathContexts have different hashcodes ",
               mcIntRm.hashCode(),
               mcStr.hashCode());
      
       assertEquals("MathContext.toString() returning incorrect value",
               mcIntRm.toString(),
               "precision=6 roundingMode=HALF_DOWN");
}
 
Example 9
Source File: BigDecimalCompareTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Abs(MathContext) of a negative BigDecimal
 */
public void testAbsMathContextNeg() {
    String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
    BigDecimal aNumber = new BigDecimal(a);
    int precision = 15;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mc = new MathContext(precision, rm);
    String result = "1.23809648392385E+53";
    int resScale = -39;
    BigDecimal res = aNumber.abs(mc);
    assertEquals("incorrect value", result, res.toString());
    assertEquals("incorrect scale", resScale, res.scale());
}
 
Example 10
Source File: RoundingModeTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 11
Source File: RoundingModeTests.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 12
Source File: RoundingModeTests.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 13
Source File: RoundingModeTests.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 14
Source File: RoundingModeTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 15
Source File: RoundingModeTests.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 16
Source File: RoundingModeTests.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 17
Source File: RoundingModeTests.java    From native-obfuscator with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 18
Source File: RoundingModeTests.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
Example 19
Source File: MathContextTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * java.math.MathContext#MathContext(...)
 */
public void test_MathContextConstruction() {
    String a = "-12380945E+61";
    BigDecimal aNumber = new BigDecimal(a);
    MathContext mcIntRm6hd = new MathContext(6, RoundingMode.HALF_DOWN);
    MathContext mcStr6hd = new MathContext("precision=6 roundingMode=HALF_DOWN");
    MathContext mcInt6 = new MathContext(6);
    MathContext mcInt134 = new MathContext(134);

    // getPrecision()
    assertEquals("MathContext.getPrecision() returns incorrect value",
            6, mcIntRm6hd.getPrecision() );
    assertEquals("MathContext.getPrecision() returns incorrect value",
            134, mcInt134.getPrecision() );

    // getRoundingMode()
    assertEquals("MathContext.getRoundingMode() returns incorrect value",
            RoundingMode.HALF_UP,
            mcInt6.getRoundingMode());
    assertEquals("MathContext.getRoundingMode() returns incorrect value",
            RoundingMode.HALF_DOWN, mcIntRm6hd.getRoundingMode() );

    // toString()
    assertEquals("MathContext.toString() returning incorrect value",
            "precision=6 roundingMode=HALF_DOWN", mcIntRm6hd.toString() );
    assertEquals("MathContext.toString() returning incorrect value",
            "precision=6 roundingMode=HALF_UP", mcInt6.toString() );

    // equals(.)
    assertEquals("Equal MathContexts are not equal ",
            mcIntRm6hd, mcStr6hd );
    assertFalse("Different MathContexts are reported as equal ",
            mcInt6.equals(mcStr6hd) );
    assertFalse("Different MathContexts are reported as equal ",
            mcInt6.equals(mcInt134) );

    // hashCode(.)
    assertEquals("Equal MathContexts have different hashcodes ",
            mcIntRm6hd.hashCode(), mcStr6hd.hashCode() );
    assertFalse("Different MathContexts have equal hashcodes ",
            mcInt6.hashCode() == mcStr6hd.hashCode() );
    assertFalse("Different MathContexts have equal hashcodes ",
            mcInt6.hashCode() == mcInt134.hashCode() );

    // other:
    BigDecimal res = aNumber.abs(mcInt6);
    assertEquals("MathContext Constructor with int precision failed",
            new BigDecimal("1.23809E+68"),
            res);
}
 
Example 20
Source File: AngleFormat.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the rounding mode to the specified value. The given mode can be one of the following:
 *
 * <table class="sis">
 *   <caption>Supported rounding modes</caption>
 *   <tr><th>Rounding mode</th>                             <th>Result</th></tr>
 *   <tr><td>{@link RoundingMode#UP        UP}</td>         <td>Round away from zero.</td></tr>
 *   <tr><td>{@link RoundingMode#DOWN      DOWN}</td>       <td>Round towards zero.</td></tr>
 *   <tr><td>{@link RoundingMode#CEILING   CEILING}</td>    <td>Round towards positive infinity.</td></tr>
 *   <tr><td>{@link RoundingMode#FLOOR     FLOOR}</td>      <td>Round towards negative infinity.</td></tr>
 *   <tr><td>{@link RoundingMode#HALF_EVEN HALF_EVEN}</td>  <td>Round towards nearest neighbor.</td></tr>
 * </table>
 *
 * The {@link RoundingMode#HALF_UP} and {@link RoundingMode#HALF_DOWN HALF_DOWN} values are not supported
 * by the current {@code AngleFormat} implementation.
 *
 * @param  mode  the new rounding mode.
 *
 * @see NumberFormat#setRoundingMode(RoundingMode)
 *
 * @since 0.8
 */
public void setRoundingMode(final RoundingMode mode) {
    ArgumentChecks.ensureNonNull("mode", mode);
    if (mode == RoundingMode.HALF_UP || mode == RoundingMode.HALF_DOWN) {
        throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedArgumentValue_1, mode));
    }
    roundingMode = mode;
}