Java Code Examples for java.math.RoundingMode#UP

The following examples show how to use java.math.RoundingMode#UP . 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: 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 2
Source File: UniformFuturesOrderPlacingStrategy.java    From java-market-maker with Apache License 2.0 6 votes vote down vote up
/**
 * @param price        price to be rounded, has to be positive
 * @param roundingMode rounding mode that should be used (only {@link RoundingMode#UP} and {@link RoundingMode#DOWN}
 *                     are supported)
 * @return price rounded to tick size
 *
 * @throws IllegalArgumentException if {@code roundingMode} is neither {@link RoundingMode#UP} nor
 *                                  {@link RoundingMode#DOWN} or {@code price} is not positive
 */
static BigDecimal roundPriceToTickSize(final BigDecimal price, final RoundingMode roundingMode, final BigDecimal tickSize) {
    checkArgument(
        roundingMode == RoundingMode.UP || roundingMode == RoundingMode.DOWN,
        "Only rounding UP or DOWN supported"
    );
    checkArgument(price.compareTo(BigDecimal.ZERO) >= 0, "price=%s < 0", price);

    final BigDecimal remainder = price.remainder(tickSize);
    if (remainder.compareTo(BigDecimal.ZERO) == 0) {
        return price;
    }

    final BigDecimal result = price.subtract(remainder);
    if (roundingMode == RoundingMode.UP) {
        return result.add(tickSize);
    } else {
        return result;
    }
}
 
Example 3
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * divide(BigDecimal, MathContext)
 */
public void testDivideBigDecimalScaleMathContextUP() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 15;
    String b = "748766876876723342238476237823787879183470";
    int bScale = 10;
    int precision = 21;
    RoundingMode rm = RoundingMode.UP;
    MathContext mc = new MathContext(precision, rm);
    String c = "49897861180.2562512996";
    int resScale = 10;
    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 4
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * divideToIntegralValue(BigDecimal, MathContext)
 */
public void testDivideToIntegralValueMathContextUP() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    int precision = 32;
    RoundingMode rm = RoundingMode.UP;
    MathContext mc = new MathContext(precision, rm);
    String c = "277923185514690367474770683";
    int resScale = 0;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result = aNumber.divideToIntegralValue(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", resScale, result.scale());
}
 
Example 5
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * divideAndRemainder(BigDecimal, MathContext)
 */
public void testDivideAndRemainderMathContextUP() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    int precision = 75;
    RoundingMode rm = RoundingMode.UP;
    MathContext mc = new MathContext(precision, rm);
    String res = "277923185514690367474770683";
    int resScale = 0;
    String rem = "1.3032693871288309587558885943391070087960319452465789990E-15";
    int remScale = 70;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result[] = aNumber.divideAndRemainder(bNumber, mc);
    assertEquals("incorrect quotient value", res, result[0].toString());
    assertEquals("incorrect quotient scale", resScale, result[0].scale());
    assertEquals("incorrect remainder value", rem, result[1].toString());
    assertEquals("incorrect remainder scale", remScale, result[1].scale());
}
 
Example 6
Source File: TestResource.java    From AVM with MIT License 5 votes vote down vote up
@Callable
public static boolean EnumHashcode() {
    boolean ret = true;
    //From NodeEnvironment
    ret = ret && (RoundingMode.UP.hashCode() == 7);

    MathContext mc = new MathContext(1, RoundingMode.UP);
    ret = ret && (mc.hashCode() == 414);

    ret = ret && (Type1.NORMAL.hashCode() == 33);
    ret = ret && (Type2.SPECIALIZED.hashCode() == 31);

    return ret;
}
 
Example 7
Source File: DurationFormatter.java    From pikatimer with GNU General Public License v3.0 5 votes vote down vote up
public static final String durationToString(Duration d, String format, String roundingMode){
    Integer precision = 0;
    String[] tokens = format.split("\\.", -1);
    if (tokens.length > 1) precision = tokens[1].length();
    
    Boolean hours = true;
    if (format.contains("[HH:]")) hours = false;
    
    RoundingMode rm = RoundingMode.HALF_EVEN;;
    if (roundingMode.equals("Down")) rm = RoundingMode.DOWN;
    if (roundingMode.equals("Half")) rm = RoundingMode.HALF_EVEN;
    if (roundingMode.equals("Up")) rm = RoundingMode.UP;
    
    return durationToString(d,precision,hours,rm);
}
 
Example 8
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Add two numbers of equal positive scales using MathContext
 */
public void testAddMathContextEqualScalePosPos() {
    String a = "1231212478987482988429808779810457634781384756794987";
    int aScale = 10;
    String b = "747233429293018787918347987234564568";
    int bScale = 10;
    String c = "1.2313E+41";
    int cScale = -37;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(5, RoundingMode.UP);
    BigDecimal result = aNumber.add(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", cScale, result.scale());
}
 
Example 9
Source File: BigDecimalArithmeticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * divide(BigDecimal, scale, RoundingMode)
 */
public void testDivideBigDecimalScaleRoundingModeUP() {
    String a = "-37361671119238118911893939591735";
    int aScale = 10;
    String b = "74723342238476237823787879183470";
    int bScale = -15;
    int newScale = 31;
    RoundingMode rm = RoundingMode.UP;
    String c = "-5.00000E-26";
    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 10
Source File: ShadowCoverageTarget.java    From AVM with MIT License 4 votes vote down vote up
public void populate() {
    aBigDecimal = new BigDecimal("1234567890");
    aBigInteger = new BigInteger("123456789000987654321");
    aMathContext = new MathContext(1);
    aRoundingMode = RoundingMode.UP;
}