Java BigDecimal jsonNumberToBigDecimal(final JsonNumber n, final int defaultValue)

Here you can find the source of jsonNumberToBigDecimal(final JsonNumber n, final int defaultValue)

Description

json Number To Big Decimal

License

Open Source License

Declaration

public static BigDecimal jsonNumberToBigDecimal(final JsonNumber n, final int defaultValue) 

Method Source Code

//package com.java2s;
/**/* ww  w  .  ja v a 2s . c  o m*/
 * This file is part of the Aerodrome package, and is subject to the 
 * terms and conditions defined in file 'LICENSE', which is part 
 * of this source code package.
 *
 * Copyright (c) 2016 All Rights Reserved, John T. Quinn III,
 * <johnquinn3@gmail.com>
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 */

import java.math.BigDecimal;

import javax.json.JsonNumber;
import javax.json.JsonObject;

public class Main {
    public static BigDecimal jsonNumberToBigDecimal(final JsonNumber n, final int defaultValue) {
        if (n == null)
            return new BigDecimal(defaultValue);

        return n.bigDecimalValue();
    }

    public static BigDecimal jsonNumberToBigDecimal(final JsonObject json, final String property,
            final int defaultValue) {
        if (json == null || property == null || property.isEmpty())
            return new BigDecimal(defaultValue);

        try {
            return jsonNumberToBigDecimal(json.getJsonNumber(property), defaultValue);
        } catch (Exception e) {
            return new BigDecimal(defaultValue);
        }
    }

    public static BigDecimal getJsonNumber(final JsonNumber n) {
        final BigDecimal b = new BigDecimal(0);
        if (n == null)
            return b;

        return n.bigDecimalValue();
    }

    public static BigDecimal getJsonNumber(final JsonObject obj, final String property) {
        try {
            return obj.getJsonNumber(property).bigDecimalValue();
        } catch (ClassCastException e) {
            return new BigDecimal(0);
        }
    }
}

Related

  1. intToBigDecimal(int i)
  2. intValue(BigDecimal a)
  3. inverse(final BigDecimal amount)
  4. invert(BigDecimal d)
  5. joinBigDecimals(List list)
  6. limitScale(BigDecimal value, int scale)
  7. matchScale(BigDecimal[] val)
  8. mean(List numbers, MathContext context)
  9. median(final BigDecimal[] bigDecimalNumbers)