Java Code Examples for java.math.RoundingMode#DOWN

The following examples show how to use java.math.RoundingMode#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: 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: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(double.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(double.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(double.class, true, new MathContext(4, RoundingMode.DOWN));
    
}
 
Example 3
Source File: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(int.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(int.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(int.class, true, new MathContext(4, RoundingMode.DOWN));
    
}
 
Example 4
Source File: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(Double.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(Double.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(Double.class, true, new MathContext(4, RoundingMode.DOWN));
    
}
 
Example 5
Source File: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(short.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(short.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(short.class, true, new MathContext(4, RoundingMode.DOWN));
    
}
 
Example 6
Source File: SIntEncoder.java    From ethdroid with MIT License 5 votes vote down vote up
@Override
public String encode(SInt toEncode) {
    BigDecimal twoCompDec = SolidityUtils.toTwosComplement(toEncode.asString());
    MathContext mc = new MathContext(0, RoundingMode.DOWN);
    BigDecimal twoCompDecRounded = twoCompDec.round(mc);
    String twoCompDecRoundedHexa = SolidityUtils.bigDecimalToHexString(twoCompDecRounded);
    String result = SolidityUtils.padLeftWithZeros(twoCompDecRoundedHexa, 64);
    return result;
}
 
Example 7
Source File: SUIntEncoder.java    From ethdroid with MIT License 5 votes vote down vote up
@Override
public String encode(SUInt toEncode) {
    BigDecimal twoCompDec = SolidityUtils.toTwosComplement(toEncode.asString());
    MathContext mc = new MathContext(0, RoundingMode.DOWN);
    BigDecimal twoCompDecRounded = twoCompDec.round(mc);
    String twoCompDecRoundedHexa = SolidityUtils.bigDecimalToHexString(twoCompDecRounded);
    String result = SolidityUtils.padLeftWithZeros(twoCompDecRoundedHexa, 64);
    return result;
}
 
Example 8
Source File: NumericFormatterFactoryTest.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testGetDecimalFormatter() throws Exception {
	NumericFormatter formatter;
	BigDecimal result;
	String input = "12345678901234567890123456789012345678901234567890.12345678901234567890123456789012345678901234567890";
	MathContext precision = new MathContext(100, RoundingMode.DOWN);
	String locale = "en";
	
	formatter = NumericFormatterFactory.getDecimalFormatter(null, (String) null);
	result = formatter.parseBigDecimal(input);
	assertEquals(new BigDecimal(input, TransformLangExecutor.MAX_PRECISION), result);
	
	formatter = NumericFormatterFactory.getDecimalFormatter(null, (String) null, 100, 50);
	result = formatter.parseBigDecimal(input);
	assertEquals(new BigDecimal(input, precision), result);
	
	formatter = NumericFormatterFactory.getDecimalFormatter(null, locale, 100, 50);
	result = formatter.parseBigDecimal(input);
	assertEquals(new BigDecimal(input, precision), result);
	
	formatter = NumericFormatterFactory.getDecimalFormatter(null, locale, 100, 50);
	result = formatter.parseBigDecimal(input);
	assertEquals(new BigDecimal(input, precision), result);
	
	formatter = NumericFormatterFactory.getDecimalFormatter("#.#", locale, 100, 50);
	result = formatter.parseBigDecimal(input);
	assertEquals(new BigDecimal(input, precision), result);
	
	formatter = NumericFormatterFactory.getDecimalFormatter("#.#", locale, 100, 50);
	result = formatter.parseBigDecimal(input);
	assertEquals(new BigDecimal(input, precision), result);
	
}
 
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 testDivideBigDecimalScaleRoundingModeDOWN() {
    String a = "-37361671119238118911893939591735";
    int aScale = 10;
    String b = "74723342238476237823787879183470";
    int bScale = 15;
    int newScale = 31;
    RoundingMode rm = RoundingMode.DOWN;
    String c = "-50000.0000000000000000000000000000000";
    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: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(Integer.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(Integer.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(Integer.class, true, new MathContext(4, RoundingMode.DOWN));
    
}
 
Example 11
Source File: Rational.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Return a string in floating point format.
 *
 * @param digits The precision (number of digits)
 * @return The human-readable version in base 10.
 */
public String toFString(int digits) {
    if (b.compareTo(BigInteger.ONE) != 0) {
        MathContext mc = new MathContext(digits, RoundingMode.DOWN);
        BigDecimal f = (new BigDecimal(a)).divide(new BigDecimal(b), mc);
        return (f.toString());
    } else {
        return a.toString();
    }
}
 
Example 12
Source File: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(BigDecimal.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(BigDecimal.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(BigDecimal.class, true, new MathContext(4, RoundingMode.DOWN));
    
}
 
Example 13
Source File: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(Byte.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(Byte.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(Byte.class, true, new MathContext(2, RoundingMode.DOWN));
    
}
 
Example 14
Source File: SimpleNumberFormatterTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.formatter = new SimpleNumberFormatter<>(Float.class, false);
    this.formatterLenient = new SimpleNumberFormatter<>(Float.class, true);
    this.formatterPrecision = new SimpleNumberFormatter<>(Float.class, true, new MathContext(4, RoundingMode.DOWN));
    
}
 
Example 15
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 16
Source File: SquareRootOfBigIntegerExample.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {

        SquareRootOfBigIntegerExample SquareRootOfBigIntegerExample =
                new SquareRootOfBigIntegerExample();
        String n = "";

        MathContext mc = new MathContext(0, RoundingMode.DOWN);
        mc = MathContext.DECIMAL32;

        BigInteger my2P100000 = new BigInteger("0");
        BigInteger two = new BigInteger("2");
        BigInteger one = new BigInteger("1");

        my2P100000 = two.shiftLeft(2000 - 1);

        System.out.println("2^2000 --  Step 1");
        System.out.println("Value of 2^2,000 " + my2P100000);
        System.out.println("");
        System.out.println("Finding the Square Root of 2^2000");

        String mys = my2P100000 + "";
        n = (mys);
        int firsttime = 0;

        BigDecimal myNumber = new BigDecimal(n);
        BigDecimal g = new BigDecimal("1");
        BigDecimal my2 = new BigDecimal("2");
        BigDecimal epsilon = new BigDecimal("0.0000000001");

        BigDecimal nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);

        //Get the value of n/g
        BigDecimal nBygPlusg = nByg.add(g);

        //Get the value of "n/g + g
        BigDecimal nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);

        //Get the value of (n/g + g)/2
        BigDecimal saveg = nBygPlusgHalf;
        firsttime = 99;

        do {
            g = nBygPlusgHalf;
            nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);
            nBygPlusg = nByg.add(g);
            nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);
            BigDecimal savegdiff = saveg.subtract(nBygPlusgHalf);

            if (savegdiff.compareTo(epsilon) == -1) {
                firsttime = 0;
            } else {
                saveg = nBygPlusgHalf;
            }

        } while (firsttime > 1);

        System.out.println(
                "For " + mys + "\nLength: " + mys.length() + "\nThe Square Root is " + saveg);
    }
 
Example 17
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 18
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 19
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 20
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");
        }
    }