Search in sources :

Example 21 with RoundingMode

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

the class BigDecimalArithmeticTest method testDivideBigDecimalScaleMathContextHALF_DOWN.

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

Example 22 with RoundingMode

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

the class BigDecimalCompareTest method testPlusMathContextPositive.

/**
     * plus(MathContext) for a positive BigDecimal
     */
public void testPlusMathContextPositive() {
    String a = "92948782094488478231212478987482988429808779810457634781384756794987";
    int aScale = 41;
    int precision = 37;
    RoundingMode rm = RoundingMode.FLOOR;
    MathContext mc = new MathContext(precision, rm);
    String c = "929487820944884782312124789.8748298842";
    int cScale = 10;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal res = aNumber.plus(mc);
    assertEquals("incorrect value", c, res.toString());
    assertEquals("incorrect scale", cScale, res.scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 23 with RoundingMode

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

the class BigDecimalArithmeticTest method testRemainderMathContextHALF_DOWN.

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

Example 24 with RoundingMode

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

the class BigDecimalArithmeticTest method testRoundMathContextPrecision0.

/**
     * round(BigDecimal, MathContext) when precision = 0
     */
public void testRoundMathContextPrecision0() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    int precision = 0;
    RoundingMode rm = RoundingMode.HALF_UP;
    MathContext mc = new MathContext(precision, rm);
    String res = "3736186567876.876578956958765675671119238118911893939591735";
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal result = aNumber.round(mc);
    assertEquals("incorrect quotient value", res, result.toString());
    assertEquals("incorrect quotient scale", aScale, result.scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 25 with RoundingMode

use of java.math.RoundingMode in project j2objc by google.

the class BigDecimalTest method testMathContextConstruction.

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

Aggregations

RoundingMode (java.math.RoundingMode)254 BigDecimal (java.math.BigDecimal)139 BigInteger (java.math.BigInteger)88 MathContext (java.math.MathContext)86 GwtIncompatible (com.google.common.annotations.GwtIncompatible)39 Test (org.testng.annotations.Test)13 NumberFormat (java.text.NumberFormat)10 MonetaryOperator (javax.money.MonetaryOperator)8 DecimalFormat (java.text.DecimalFormat)6 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 MonetaryContext (javax.money.MonetaryContext)2 ButtonGroup (javax.swing.ButtonGroup)2 JLabel (javax.swing.JLabel)2