Java Code Examples for java.math.BigDecimal

The following are top voted examples for showing how to use java.math.BigDecimal. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: Rey-MusicPlayer   File: RangeSeekBar.java   View source code 6 votes vote down vote up
public static <E extends Number> NumberType fromNumber(E value) throws IllegalArgumentException {
    if (value instanceof Long) {
        return LONG;
    }
    if (value instanceof Double) {
        return DOUBLE;
    }
    if (value instanceof Integer) {
        return INTEGER;
    }
    if (value instanceof Float) {
        return FLOAT;
    }
    if (value instanceof Short) {
        return SHORT;
    }
    if (value instanceof Byte) {
        return BYTE;
    }
    if (value instanceof BigDecimal) {
        return BIG_DECIMAL;
    }
    throw new IllegalArgumentException("Number class '" + value.getClass().getName() + "' is not supported");
}
 
Example 2
Project: SoftUni   File: p04_EuroTrip.java   View source code 6 votes vote down vote up
public static void main(String[] args){
    // Create new scanner
    Scanner input = new Scanner(System.in);
    // Read the quantity as a double value
    double wurst = input.nextDouble();
    double pricePerKilo = 1.20;
    BigDecimal priceInBGN = new BigDecimal((pricePerKilo * wurst));
    // Multiply the price in BGN with price in DM
    BigDecimal exchangeRate = new BigDecimal("4210500000000");
    BigDecimal neededDM = exchangeRate.multiply(priceInBGN);

    System.out.printf("%.2f marks", neededDM);
}
 
Example 3
Project: oscm   File: BillingServiceBeanIT.java   View source code 5 votes vote down vote up
@Test
public void testChangeUserAssignmentPeriodPricesForUsedPriceModelBeginOfJanuary()
        throws Exception {
    final int testMonth = Calendar.JANUARY;
    final int testDay = 1;
    BigDecimal etalonPrice = P_1_PRICE_PER_PERIOD
            .add(new BigDecimal(12345));

    final long billingTime = getBillingTime(testYear, testMonth, testDay);

    creSub(P_1_ID, getDate(testYear, testMonth, -2, 8, 0));
    subAddUser(SUBSCRIPTION_ID, "anton",
            getDate(testYear, testMonth, -2, 8, 0));

    // now change the price for the period on the last day of the month in
    // the billing period. Finally, the price must be reflected in the costs
    // - for the entire period.
    updSubscriptionPrices(P_1_PRICE_LOGIN, P_1_PRICE_UPLOAD,
            P_1_PRICE_LOGOUT, getDate(testYear, testMonth, 27, 23, 59),
            P_1_PRICE_PER_PERIOD, new BigDecimal(12345));

    updSubscriptionPrices(P_1_PRICE_LOGIN, P_1_PRICE_UPLOAD,
            P_1_PRICE_LOGOUT, getDate(testYear, testMonth, 35, 23, 59),
            P_1_PRICE_PER_PERIOD, P_1_PRICE_PER_USER);

    startBillingRun(billingTime);

    Date periods[][] = new Date[][] { { getStartDate(testYear, testMonth),
            getEndDate(testYear, testMonth) } };

    verify(periods, etalonPrice, testMonth);
    xmlValidator.validateBillingResultXML();
}
 
Example 4
Project: ukubuka-core   File: UkubukaTransformerTest.java   View source code 5 votes vote down vote up
@Test
public void test_performOperations_avg_success()
        throws PipelineException, TransformException {
    TransformOperations transformOperation = new TransformOperations();
    transformOperation.setType(TransformOperation.AVG);
    transformOperation.setSource("foobar");
    transformOperation.setTarget("data[1]");

    List<String> fileHeader = new ArrayList<>(Arrays.asList("foo", "bar"));
    List<FileRecord> fileRecords = new ArrayList<>(Arrays.asList(
            new FileRecord(new ArrayList<>(
                    Arrays.asList("bar", String.valueOf("3")))),
            new FileRecord(new ArrayList<>(
                    Arrays.asList("foo", String.valueOf("3"))))));
    List<TransformOperations> operationsList = new ArrayList<>(
            Arrays.asList(transformOperation));

    Mockito.when(
            expressionEvaluator.evaluate(Mockito.any(FileContents.class),
                    Mockito.any(FileRecord.class), Mockito.anyString()))
            .thenReturn(new BigDecimal(3));

    Transform transforms = new Transform();
    transforms.setId("foo-X");
    TransformOperationsType transformOperationsType = new TransformOperationsType();
    transformOperationsType.setColumn(operationsList);
    transforms.setOperations(transformOperationsType);
    Map<String, FileContents> dataFiles = new HashMap<>();
    dataFiles.put("foo-X", new FileContents(fileHeader, fileRecords));

    UkubukaSchema ukubukaSchema = new UkubukaSchema();
    ukubukaSchema.setTransforms(Arrays.asList(transforms));

    ukubukaTransformer.performOperations(dataFiles, ukubukaSchema);

    Mockito.verify(expressionEvaluator, Mockito.times(2)).evaluate(
            Mockito.any(FileContents.class), Mockito.any(FileRecord.class),
            Mockito.anyString());
    assertNotNull(dataFiles.get("foo-X").getAggregations().get("foobar"));
}
 
Example 5
Project: openjdk-systemtest   File: TestSuite027.java   View source code 5 votes vote down vote up
public void testItem_0183()
{
  rc_byte = (new BigDecimal("0")).byteValueExact();
  Assert.assertEquals(0, rc_byte);
}
 
Example 6
Project: openjdk-systemtest   File: TestSuite034.java   View source code 5 votes vote down vote up
public void testItem_0688()
{
  boolean caught;
  caught = false;
  try {
    rc_BigDecimal = (new BigDecimal("-3.2E-2147483646")).pow(-2147483648);
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.94630872483222%", true, caught);
}
 
Example 7
Project: openjdk-systemtest   File: TestSuite003.java   View source code 5 votes vote down vote up
public void testItem_0869()
{
  boolean caught;
  caught = false;
  try {
    rc_BigDecimal = new BigDecimal("J>[email protected];9CNPW>X>EIS>Q;J>6VX1VN:BK>[email protected]<7JAUNJB9AK7404:CTHY9CRAM:X1S3KYESVSHQXH1L>2YH", new MathContext("precision=2147483647 roundingMode=HALF_UP"));
  }
  catch (java.lang.NumberFormatException e) {
    caught = true;
  }
  Assert.assertEquals("89.93288590604027%", true, caught);
}
 
Example 8
Project: openjdk-systemtest   File: TestSuite054.java   View source code 5 votes vote down vote up
public void testItem_0227()
{
  boolean caught;
  caught = false;
  try {
    rc_BigDecimal = (new BigDecimal("-321")).remainder(new BigDecimal("0.0"));
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.94630872483222%", true, caught);
}
 
Example 9
Project: openjdk-systemtest   File: TestSuite012.java   View source code 5 votes vote down vote up
public void testItem_0899()
{
  boolean caught;
  caught = false;
  try {
    rc_long = (new BigDecimal("-1.8")).longValueExact();
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.61073825503355%", true, caught);
}
 
Example 10
Project: openjdk-systemtest   File: TestSuite025.java   View source code 5 votes vote down vote up
public void testItem_0425()
{
  boolean caught;
  caught = false;
  try {
    rc_long = (new BigDecimal("1E-2147483647")).longValueExact();
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.94630872483222%", true, caught);
}
 
Example 11
Project: openjdk-systemtest   File: TestSuite036.java   View source code 5 votes vote down vote up
public void testItem_0261()
{
  boolean caught;
  caught = false;
  try {
    rc_BigDecimal = (new BigDecimal("1")).pow(2147483647, new MathContext("precision=1 roundingMode=HALF_UP"));
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.94630872483222%", true, caught);
}
 
Example 12
Project: openjdk-systemtest   File: TestSuite015.java   View source code 5 votes vote down vote up
public void testItem_0205()
{
  boolean caught;
  caught = false;
  try {
    rc_int = (new BigDecimal("-147573952452237459488.0E+2147483647")).intValueExact();
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.61073825503355%", true, caught);
}
 
Example 13
Project: openjdk-systemtest   File: TestSuite052.java   View source code 5 votes vote down vote up
public void testItem_0512()
{
  boolean caught;
  caught = false;
  try {
    rc_BigDecimal = new BigDecimal("", new MathContext("precision=2147483647 roundingMode=HALF_UP"));
  }
  catch (java.lang.NumberFormatException e) {
    caught = true;
  }
  Assert.assertEquals("91.94630872483222%", true, caught);
}
 
Example 14
Project: openjdk-systemtest   File: TestSuite052.java   View source code 5 votes vote down vote up
public void testItem_0401()
{
  boolean caught;
  caught = false;
  try {
    rc_BigDecimal = (new BigDecimal("-2")).pow(-1, new MathContext("precision=2147483647 roundingMode=FLOOR"));
  }
  catch (java.lang.IllegalArgumentException e) {
    caught = true;
  }
  Assert.assertEquals("91.94630872483222%", true, caught);
}
 
Example 15
Project: openjdk-systemtest   File: TestSuite035.java   View source code 5 votes vote down vote up
public void testItem_0120()
{
  boolean caught;
  caught = false;
  try {
    rc_short = (new BigDecimal("2147483647")).shortValueExact();
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.94630872483222%", true, caught);
}
 
Example 16
Project: openjdk-systemtest   File: TestSuite012.java   View source code 5 votes vote down vote up
public void testItem_0709()
{
  boolean caught;
  caught = false;
  try {
    rc_BigDecimal = (new BigDecimal("2E+9")).setScale(2147483647);
  }
  catch (java.lang.ArithmeticException e) {
    caught = true;
  }
  Assert.assertEquals("91.61073825503355%", true, caught);
}
 
Example 17
Project: openjdk-systemtest   File: TestSuite025.java   View source code 4 votes vote down vote up
public void testItem_0982()
{
  rc_BigDecimal = (new BigDecimal("3.2E+2")).plus(new MathContext("precision=2147483647 roundingMode=HALF_UP"));
  Assert.assertEquals("3.2E+2", rc_BigDecimal.toString());
}
 
Example 18
Project: openjdk-systemtest   File: TestSuite017.java   View source code 4 votes vote down vote up
public void testItem_0682()
{
  rc_BigDecimal = (new BigDecimal("-1")).movePointLeft(1);
  Assert.assertEquals("-0.1", rc_BigDecimal.toString());
}
 
Example 19
Project: openjdk-systemtest   File: TestSuite029.java   View source code 4 votes vote down vote up
public void testItem_0524()
{
  rc_BigDecimal = (new BigDecimal("0.0")).setScale(-2147483648, java.math.RoundingMode.HALF_DOWN);
  Assert.assertEquals("0E+2147483648", rc_BigDecimal.toString());
}
 
Example 20
Project: GitHub   File: TestASM_BigDecimal.java   View source code 4 votes vote down vote up
public BigDecimal getDecimal() {
    return decimal;
}
 
Example 21
Project: openjdk-systemtest   File: TestSuite002.java   View source code 4 votes vote down vote up
public void testItem_0034()
{
  rc_BigInteger = (new BigDecimal("0")).unscaledValue();
  Assert.assertEquals("0", rc_BigInteger.toString());
}
 
Example 22
Project: openjdk-systemtest   File: TestSuite007.java   View source code 4 votes vote down vote up
public void testItem_0455()
{
  rc_BigDecimal = new BigDecimal(-1.0);
  Assert.assertEquals("-1", rc_BigDecimal.toString());
}
 
Example 23
Project: openjdk-systemtest   File: TestSuite009.java   View source code 4 votes vote down vote up
public void testItem_0710()
{
  rc_int = (new BigDecimal("1")).scale();
  Assert.assertEquals(0, rc_int);
}
 
Example 24
Project: openjdk-systemtest   File: TestSuite050.java   View source code 4 votes vote down vote up
public void testItem_0518()
{
  rc_BigDecimal = (new BigDecimal("0")).plus();
  Assert.assertEquals("0", rc_BigDecimal.toString());
}
 
Example 25
Project: openjdk-systemtest   File: TestSuite059.java   View source code 4 votes vote down vote up
public void testItem_0604()
{
  rc_int = (new BigDecimal("-214748365.7")).scale();
  Assert.assertEquals(1, rc_int);
}
 
Example 26
Project: openjdk-systemtest   File: TestSuite000.java   View source code 4 votes vote down vote up
public void testItem_0193()
{
  rc_BigDecimal = new BigDecimal(1.7976931348623157E308, new MathContext("precision=1 roundingMode=HALF_UP"));
  Assert.assertEquals("2E+308", rc_BigDecimal.toString());
}
 
Example 27
Project: Hydrograph   File: NumericFunctions.java   View source code 4 votes vote down vote up
/**
 * Rounds the {@code inputValue} to specified number of digits to the right of the decimal point.
 * This function always rounds up the number irrespective of the "{@code numberOfDigits} + 1" digit.
 *
 * @param inputValue     the float value to be rounded up
 * @param numberOfDigits the number of digits to round up the {@code inputValue}
 * @return rounded up {@code inputValue} value
 */
public static Float roundUp(Float inputValue, int numberOfDigits) {
    if (inputValue == null)
        return null;
    BigDecimal bigDecimal = new BigDecimal(inputValue);
    bigDecimal = bigDecimal.setScale(numberOfDigits, BigDecimal.ROUND_UP);
    return bigDecimal.floatValue();
}
 
Example 28
Project: openjdk-systemtest   File: TestSuite018.java   View source code 4 votes vote down vote up
public void testItem_0740()
{
  rc_BigDecimal = (new BigDecimal("32")).abs();
  Assert.assertEquals("32", rc_BigDecimal.toString());
}
 
Example 29
Project: openjdk-systemtest   File: TestSuite041.java   View source code 4 votes vote down vote up
public void testItem_0755()
{
  rc_BigDecimal = new BigDecimal(new java.math.BigInteger("32"), new MathContext("precision=2147483647 roundingMode=HALF_EVEN"));
  Assert.assertEquals("32", rc_BigDecimal.toString());
}
 
Example 30
Project: openjdk-systemtest   File: TestSuite010.java   View source code 4 votes vote down vote up
public void testItem_0195()
{
  rc_BigInteger = (new BigDecimal("1")).toBigInteger();
  Assert.assertEquals("1", rc_BigInteger.toString());
}
 
Example 31
Project: openjdk-systemtest   File: TestSuite043.java   View source code 4 votes vote down vote up
public void testItem_0228()
{
  rc_BigDecimal = (new BigDecimal("3E+1")).pow(0, new MathContext("precision=1 roundingMode=HALF_UP"));
  Assert.assertEquals("1", rc_BigDecimal.toString());
}
 
Example 32
Project: openjdk-systemtest   File: TestSuite040.java   View source code 4 votes vote down vote up
public void testItem_0880()
{
  rc_BigDecimal = (new BigDecimal("-1E-2147483647")).negate(new MathContext("precision=2147483647 roundingMode=UP"));
  Assert.assertEquals("1E-2147483647", rc_BigDecimal.toString());
}
 
Example 33
Project: openjdk-systemtest   File: TestSuite041.java   View source code 4 votes vote down vote up
public void testItem_0683()
{
  rc_BigDecimal = new BigDecimal(new java.math.BigInteger("32"), new MathContext("precision=0 roundingMode=HALF_UP"));
  Assert.assertEquals("32", rc_BigDecimal.toString());
}
 
Example 34
Project: openjdk-systemtest   File: TestSuite008.java   View source code 4 votes vote down vote up
public void testItem_0959()
{
  rc_BigDecimal = (new BigDecimal("1")).multiply(new BigDecimal("1"));
  Assert.assertEquals("1", rc_BigDecimal.toString());
}
 
Example 35
Project: openjdk-systemtest   File: TestSuite035.java   View source code 4 votes vote down vote up
public void testItem_0516()
{
  rc_String = (new BigDecimal("0")).toEngineeringString();
  Assert.assertEquals("0", rc_String);
}
 
Example 36
Project: openjdk-systemtest   File: TestSuite034.java   View source code 4 votes vote down vote up
public void testItem_0825()
{
  rc_BigDecimal = new BigDecimal(1, new MathContext("precision=1 roundingMode=FLOOR"));
  Assert.assertEquals("1", rc_BigDecimal.toString());
}
 
Example 37
Project: openjdk-systemtest   File: TestSuite038.java   View source code 4 votes vote down vote up
public void testItem_0679()
{
  rc_BigDecimal = (new BigDecimal("0")).movePointLeft(-2147483648);
  Assert.assertEquals("0", rc_BigDecimal.toString());
}
 
Example 38
Project: openjdk-systemtest   File: TestSuite019.java   View source code 4 votes vote down vote up
public void testItem_0277()
{
  rc_BigDecimal_array = (new BigDecimal("1")).divideAndRemainder(new BigDecimal("1"));
}
 
Example 39
Project: openjdk-systemtest   File: TestSuite029.java   View source code 4 votes vote down vote up
public void testItem_0594()
{
  rc_BigDecimal = new BigDecimal(new java.math.BigInteger("32"), new MathContext("precision=1 roundingMode=HALF_UP"));
  Assert.assertEquals("3E+1", rc_BigDecimal.toString());
}
 
Example 40
Project: openjdk-systemtest   File: TestSuite048.java   View source code 4 votes vote down vote up
public void testItem_0260()
{
  rc_String = (new BigDecimal("-3E+2")).toPlainString();
  Assert.assertEquals("-300", rc_String);
}