Example usage for java.math BigDecimal longValue

List of usage examples for java.math BigDecimal longValue

Introduction

In this page you can find the example usage for java.math BigDecimal longValue.

Prototype

@Override
public long longValue() 

Source Link

Document

Converts this BigDecimal to a long .

Usage

From source file:org.apache.drill.exec.planner.sql.handlers.SetOptionHandler.java

private static Object sqlLiteralToObject(final SqlLiteral literal) {
    final Object object = literal.getValue();
    final SqlTypeName typeName = literal.getTypeName();
    switch (typeName) {
    case DECIMAL: {
        final BigDecimal bigDecimal = (BigDecimal) object;
        if (bigDecimal.scale() == 0) {
            return bigDecimal.longValue();
        } else {// w ww  .  j a  v a  2  s.  c o  m
            return bigDecimal.doubleValue();
        }
    }

    case DOUBLE:
    case FLOAT:
        return ((BigDecimal) object).doubleValue();

    case SMALLINT:
    case TINYINT:
    case BIGINT:
    case INTEGER:
        return ((BigDecimal) object).longValue();

    case VARBINARY:
    case VARCHAR:
    case CHAR:
        return ((NlsString) object).getValue().toString();

    case BOOLEAN:
        return object;

    default:
        throw UserException.validationError()
                .message("Drill doesn't support assigning literals of type %s in SET statements.", typeName)
                .build(logger);
    }
}

From source file:org.apache.phoenix.util.DateUtil.java

/**
 * Utility function to convert a {@link BigDecimal} value to {@link Timestamp}.
 *//* w  w w .  j a v  a2s.  co  m*/
public static Timestamp getTimestamp(BigDecimal bd) {
    return DateUtil.getTimestamp(bd.longValue(),
            ((bd.remainder(BigDecimal.ONE).multiply(BigDecimal.valueOf(MILLIS_TO_NANOS_CONVERTOR)))
                    .intValue()));
}

From source file:com.nortal.petit.converter.util.ResultSetHelper.java

public static Long getLong(ResultSet rs, ColumnPosition column) throws SQLException {
    BigDecimal bd = getBigDecimal(rs, column);
    return bd == null ? null : Long.valueOf(bd.longValue());
}

From source file:org.osaf.cosmo.eim.schema.EimFieldValidator.java

/**
 * Validates and returns a timestamp field value.
 *
 * @throws EimValidationException if the value is invalid
 *//*  w  ww  .j  a va2 s  . co m*/
public static Date validateTimeStamp(EimRecordField field) throws EimValidationException {
    BigDecimal bd = validateDecimal(field, DIGITS_TIMESTAMP, DEC_TIMESTAMP);
    if (bd == null)
        return null;
    // a unix timestamp is the number of seconds since the epoch,
    // but Date wants milliseconds
    Date value = new Date(bd.longValue() * 1000);
    return value;
}

From source file:nl.strohalm.cyclos.utils.DateHelper.java

/**
 * inverse function of decimalDaysBetween: adds a BigDecimal number of days to a Calendar.
 * @param date1 the date to which something is added
 * @param augend the number of days which is added, as a BigDecimal. May be negative.
 * @return null if one of the arguments is null, else the date + augend days.
 *//* w w  w .  ja v a  2  s .c  o m*/
public static Calendar decimalDaysAdd(final Calendar date1, final BigDecimal augend) {
    if (date1 == null || augend == null) {
        return null;
    }
    final BigDecimal date1InMillis = new BigDecimal(date1.getTimeInMillis());
    final BigDecimal augendInMillis = augend.multiply(new BigDecimal(DateUtils.MILLIS_PER_DAY));
    final BigDecimal resultAsBig = date1InMillis.add(augendInMillis);
    final Calendar result = Calendar.getInstance();
    result.setTimeInMillis(resultAsBig.longValue());
    return result;
}

From source file:helpers.Methods.java

/**
 * This method converts human readable file size to bytes (1024).
 *
 * @param humanReadableFileSize The human readable file size (Ex. 500MB
 * <b>without space between size and unit</b>)
 * @return the bytes of human readable size in long format.
 *///  ww  w . java2 s  . c  o  m
public static long filesizeToBytes(String humanReadableFileSize) {
    long returnValue = -1;
    java.util.regex.Pattern patt = java.util.regex.Pattern.compile("([\\d.]+)([GMK]B)",
            java.util.regex.Pattern.CASE_INSENSITIVE);
    Matcher matcher = patt.matcher(humanReadableFileSize);
    Map<String, Integer> powerMap = new HashMap<>();
    powerMap.put("TB", 4);
    powerMap.put("GB", 3);
    powerMap.put("MB", 2);
    powerMap.put("KB", 1);
    powerMap.put("B", 0);
    if (matcher.find()) {
        String number = matcher.group(1);
        int pow = powerMap.get(matcher.group(2).toUpperCase());
        BigDecimal bytes = new BigDecimal(number);
        bytes = bytes.multiply(BigDecimal.valueOf(1024).pow(pow));
        returnValue = bytes.longValue();
    }
    return returnValue;
}

From source file:com.fanniemae.ezpie.common.StringUtilities.java

public static String formatAsNumber(double value) {
    BigDecimal valueOf = BigDecimal.valueOf(value);
    BigDecimal integerPart = BigDecimal.valueOf(valueOf.longValue());
    BigDecimal fractional = valueOf.subtract(integerPart);
    String fraction = fractional.toPlainString();
    int indexOfDot = fraction.indexOf('.') + 1;
    String sign = fraction.startsWith("-") ? "-" : "";
    fraction = fraction.length() > indexOfDot ? fraction.substring(indexOfDot) : fraction;
    if (fraction.equals("0")) {
        return MessageFormat.format("{0}{1}", sign, integerPart.toPlainString(), fraction);
    } else {//  w w w .  j a  v a 2s .  c  o  m
        return MessageFormat.format("{0}{1}.{2}", sign, integerPart.toPlainString(), fraction);
    }
}

From source file:org.auraframework.component.test.java.controller.TestControllerLocalization.java

/**
 * Wait for delayMs milliseconds and then return a auratest:text component
 * whose value is the current buffer contents plus the current append.
 *//*from  ww w .j a v a2s  . co m*/
@AuraEnabled
public static Component appendBuffer(@Key("id") String id, @Key("delayMs") BigDecimal delayMs,
        @Key("append") String append) throws Exception {
    StringBuffer buffer = buffers.get(id);
    buffer.append(append);
    long delay = delayMs.longValue();
    if (delay > 0) {
        Thread.sleep(delay);
    }
    Map<String, Object> atts = ImmutableMap.of("value", (Object) (buffer + "."));
    return Aura.getInstanceService().getInstance("auratest:text", ComponentDef.class, atts);
}

From source file:com.breadwallet.tools.security.RequestHandler.java

private static boolean tryAndProcessBitcoinURL(RequestObject requestObject, MainActivity app) {
    /** use the C implementation to check it */
    final String str = requestObject.address;
    if (str == null)
        return false;
    final String[] addresses = new String[1];
    addresses[0] = str;// www  .  j  a v a  2  s . c o  m
    if (requestObject.amount != null) {
        BigDecimal bigDecimal = new BigDecimal(requestObject.amount);
        long amount = bigDecimal.longValue();
        if (amount == 0 && app != null) {
            app.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    FragmentScanResult.address = str;
                    //TODO find a better way
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            BRAnimator.animateScanResultFragment();
                        }
                    }, 500);

                }
            });
            return false;
        }
        String strAmount = String.valueOf(amount);
        if (app != null) {
            BRWalletManager.getInstance(app).pay(addresses[0], new BigDecimal(strAmount), null, true);
        }
    } else {
        if (app != null)
            app.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    FragmentScanResult.address = str;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            BRAnimator.animateScanResultFragment();
                        }
                    }, 1000);
                }
            });
    }
    return true;
}

From source file:org.apache.hadoop.hive.serde2.io.TimestampWritable.java

public static Timestamp decimalToTimestamp(BigDecimal d) {
    BigDecimal seconds = new BigDecimal(d.longValue());
    long millis = d.multiply(new BigDecimal(1000)).longValue();
    int nanos = d.subtract(seconds).multiply(new BigDecimal(1000000000)).intValue();

    Timestamp t = new Timestamp(millis);
    t.setNanos(nanos);/*from   ww  w.  j  av  a  2  s  .  c o  m*/

    return t;
}