Java 8 MathContext - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 MathContext class.

This code example shows how to use the following methods:equals, getRoundingMode, hashCode, toString
    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() );

7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getPrecision, getRoundingMode
     */ 
    @SuppressWarnings({"JavaDoc"}) 
    private static BigDecimal sqrtProcedure(MathContext mc, int digits, BigDecimal numberToBeSquareRooted, BigDecimal iteration1,
                                            BigDecimal iteration2, BigDecimal temp1, BigDecimal temp2) {
        // next v                                         // g = 1 - 2*x*v 
        temp1 = BigDecimal.ONE.subtract(TWO.multiply(iteration1, mc).multiply(iteration2, mc), mc);
        iteration2 = iteration2.add(temp1.multiply(iteration2, mc), mc); // v += g*v        ~ 1/2/sqrt(d)
 
        // next x 
        temp2 = numberToBeSquareRooted.subtract(iteration1.multiply(iteration1, mc), mc); // e = d - x^2
        iteration1 = iteration1.add(temp2.multiply(iteration2, mc), mc); // x += e*v        ~ sqrt(d)
 
        // increase precision 
        int m = mc.getPrecision();
        if (m < 2)
            m++;
        else 
            m = m * 2 - 1; // next Newton iteration supplies so many exact digits
 
        if (m < 2 * digits) // digits limit not yet reached?

7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
 
public class FixedPercentageAllocationStrategy implements MoneyManagementStrategy { 
 
    private BigDecimal ratio;
    private Asset asset;
    private final MathContext mc = new MathContext(10, RoundingMode.DOWN);
 
    public FixedPercentageAllocationStrategy(double ratio, Asset asset) throws Exception {
        if (ratio > 1.0) {
            throw new Exception("Cannot risk more than all of account");
        } else if (ratio <= 0.0) {
            throw new Exception("Ratio must be > 0");
        } 
        this.ratio = new BigDecimal(ratio);
        this.asset = asset;
    } 
 
    @Override 
    public int sizePosition(Account account, DateTime time) {
        BigDecimal price = asset.priceAt(time);
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:equals, getRoundingMode, hashCode, toString
    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() );

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getPrecision, getRoundingMode
	} 
 
	@Override 
	public void writeExternal(Object o, ObjectOutput out) throws IOException, IllegalAccessException {
		MathContext mc = (MathContext)o;
		out.writeObject(mc.getPrecision());
		out.writeObject(mc.getRoundingMode());
	} 
} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
        // verification 
        assertThat(doubleOf(durationInYears), is(doubleOf(Double.valueOf("0.2333"))));
    } 
 
    private Double doubleOf(Double durationInYears) {
        MathContext mc = new MathContext(4);
        return BigDecimal.valueOf(durationInYears).round(mc).doubleValue();
    } 
} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:hashCode, toString
	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); 

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getPrecision, getRoundingMode
     
    MathContext mc2 = new MathContext("precision=17 roundingMode=DOWN"); 
    assertTrue(mc2.getPrecision() == 17);
    assertTrue(mc2.getRoundingMode().equals(RoundingMode.DOWN));
     
    MathContext mc3 = new MathContext("precision=18 roundingMode=FLOOR"); 
    assertTrue(mc3.getPrecision() == 18);
    assertTrue(mc3.getRoundingMode().equals(RoundingMode.FLOOR));
     
    MathContext mc4 = new MathContext("precision=19 roundingMode=HALF_DOWN"); 
    assertTrue(mc4.getPrecision() == 19);
    assertTrue(mc4.getRoundingMode().equals(RoundingMode.HALF_DOWN));
     
    MathContext mc5 = new MathContext("precision=20 roundingMode=HALF_EVEN"); 
    assertTrue(mc5.getPrecision() == 20);
    assertTrue(mc5.getRoundingMode().equals(RoundingMode.HALF_EVEN));
     
    MathContext mc6 = new MathContext("precision=21 roundingMode=HALF_UP"); 
    assertTrue(mc6.getPrecision() == 21);
    assertTrue(mc6.getRoundingMode().equals(RoundingMode.HALF_UP));
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
        // insure that we are using the correct internal precision 
        BigDecimal roundingAmount = roundOffMultiple.setScale(internalPrecision, internalRoundingMode);
        // FIXME: are we loosing precision here 
        // mathcontext only take cares of significant digits 
        // not digit right to the decimal 
        BigDecimal nearestFactor = money.getAmount().divide(roundingAmount,
                new MathContext(internalPrecision, internalRoundingMode));
 
        nearestFactor = nearestFactor.setScale(0, roundingMode);
 
        BigDecimal roundedAmount = nearestFactor.multiply(roundingAmount);
        return new Money(money.getCurrency(), roundedAmount);
    } 
 
    /** 
     * This method return true if the currency associated with the two money 
     * objects is equal and also the compareTo method of BigDecimal return 0 for 
     * the amount of the two money objects. It is not advisable to use equals 
     * method of BigDecimal because it would return false for numbers like 10.0 
     * and 10.00 instead we should use compareTo. 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:equals, getRoundingMode, hashCode, toString
    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() );

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Java 8 MathContext Questions & Answers:

See Code Examples for Java 8 MathContext Methods: