Search in sources :

Example 76 with RoundingMode

use of java.math.RoundingMode in project robovm by robovm.

the class OldBigDecimalConstructorsTest method testConstrCharIntIntMathContextException1.

/**
     * new BigDecimal(char[] value, int offset, int len, MathContext mc);
     */
public void testConstrCharIntIntMathContextException1() {
    char[] value = { '-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3' };
    int offset = 3;
    int len = 120;
    int precision = 4;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    try {
        new BigDecimal(value, offset, len, mc);
        fail("NumberFormatException has not been thrown");
    } catch (NumberFormatException e) {
    }
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 77 with RoundingMode

use of java.math.RoundingMode in project robovm by robovm.

the class OldBigDecimalConstructorsTest method testConstrStringMathContext.

/**
     * new BigDecimal(String value, MathContext)
     */
public void testConstrStringMathContext() {
    String a = "-238768787678287e214";
    int precision = 5;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String res = "-23876";
    int resScale = -224;
    BigDecimal result = new BigDecimal(a, mc);
    assertEquals("incorrect value", res, result.unscaledValue().toString());
    assertEquals("incorrect scale", resScale, result.scale());
    // Now test more than just RoundingMode.CEILING:
    //
    String biStr = new String("12345678901234567890123456789012345.0E+10");
    String nbiStr = new String("-12345678901234567890123456789012345.E+10");
    BigDecimal bd;
    mc = new MathContext(31, RoundingMode.UP);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.234567890123456789012345678902E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
    mc = new MathContext(28, RoundingMode.DOWN);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.234567890123456789012345678E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
    mc = new MathContext(33, RoundingMode.CEILING);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.23456789012345678901234567890124E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
    mc = new MathContext(34, RoundingMode.UNNECESSARY);
    try {
        bd = new BigDecimal(biStr, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    try {
        bd = new BigDecimal(nbiStr, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    mc = new MathContext(7, RoundingMode.FLOOR);
    bd = new BigDecimal("1000000.9", mc);
    assertEquals("incorrect value", "1000000", bd.toString());
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 78 with RoundingMode

use of java.math.RoundingMode in project robovm by robovm.

the class OldBigDecimalArithmeticTest method test_DivideBigDecimalRoundingExc.

/**
     * java.math.BigDecimal#divide(java.math.BigDecimal,
     *        java.math.RoundingMode) divide(BigDecimal, RoundingMode)
     */
public void test_DivideBigDecimalRoundingExc() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    String b = "74723342238476237823787879183470";
    RoundingMode rm = RoundingMode.UNNECESSARY;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a));
    BigDecimal bNumber = new BigDecimal(new BigInteger(b));
    try {
        aNumber.divide(bNumber, rm);
        fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY divider");
    } catch (java.lang.ArithmeticException ae) {
    // expected
    }
    try {
        bNumber = new BigDecimal(0);
        aNumber.divide(bNumber, rm);
        fail("ArithmeticException is not thrown for zero divider");
    } catch (java.lang.ArithmeticException ae) {
    // expected
    }
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 79 with RoundingMode

use of java.math.RoundingMode in project robovm by robovm.

the class OldBigDecimalArithmeticTest method test_DivideBigDecimalRoundingModeCEILING.

/**
     * java.math.BigDecimal#divide(java.math.BigDecimal,
     *        java.math.RoundingMode) divide(BigDecimal, RoundingMode)
     */
public void test_DivideBigDecimalRoundingModeCEILING() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    String b = "74723342238476237823787879183470";
    RoundingMode rm = RoundingMode.CEILING;
    String c = "50000260373164286401361914";
    BigDecimal aNumber = new BigDecimal(new BigInteger(a));
    BigDecimal bNumber = new BigDecimal(new BigInteger(b));
    BigDecimal result = aNumber.divide(bNumber, rm);
    assertEquals("incorrect value", c, result.toString());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 80 with RoundingMode

use of java.math.RoundingMode in project robovm by robovm.

the class BigDecimalArithmeticTest method testDivideBigDecimalScaleRoundingModeHALF_DOWN.

/**
     * 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());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Aggregations

RoundingMode (java.math.RoundingMode)206 BigDecimal (java.math.BigDecimal)127 BigInteger (java.math.BigInteger)88 MathContext (java.math.MathContext)72 GwtIncompatible (com.google.common.annotations.GwtIncompatible)39 DecimalFormat (java.text.DecimalFormat)5 IDataMap (permafrost.tundra.data.IDataMap)5 Test (org.junit.Test)3 MifosConfigurationManager (org.mifos.config.business.MifosConfigurationManager)3 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Insets (java.awt.Insets)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 ButtonGroup (javax.swing.ButtonGroup)2 JLabel (javax.swing.JLabel)2 JPanel (javax.swing.JPanel)2 JRadioButton (javax.swing.JRadioButton)2 JSpinner (javax.swing.JSpinner)2 SpinnerNumberModel (javax.swing.SpinnerNumberModel)2