Example usage for java.math BigDecimal toString

List of usage examples for java.math BigDecimal toString

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns the string representation of this BigDecimal , using scientific notation if an exponent is needed.

Usage

From source file:com.spaceprogram.simplejpa.util.AmazonSimpleDBUtil.java

public static String encodeRealNumberRange(BigDecimal number, int maxNumDigits, BigDecimal offsetValue) {
    BigDecimal offsetNumber = number.add(offsetValue);
    String longString = offsetNumber.toString();
    int numZeroes = maxNumDigits - longString.length();
    StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
    for (int i = 0; i < numZeroes; i++) {
        strBuffer.insert(i, '0');
    }//from www  .j a  v  a 2  s.co m
    strBuffer.append(longString);
    return strBuffer.toString();
}

From source file:es.tena.foundation.util.POIUtil.java

public static String getCelda(HSSFCell cellSugg) {
    String suggestion = "";
    if (cellSugg != null) {
        if (cellSugg.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            suggestion = StringUtil.trim(StringEscapeUtils.escapeSql(cellSugg.getStringCellValue()));
        } else if (cellSugg.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            BigDecimal big = new BigDecimal(cellSugg.getNumericCellValue());
            suggestion = big.toString();
        } // no hace falta else
        else {/*from ww  w.j av a 2 s .c o m*/
            suggestion = StringUtil.trim(StringEscapeUtils.escapeSql(cellSugg.getStringCellValue()));
        }
        suggestion = SQLUtil.replace(suggestion);
    }
    return suggestion;
}

From source file:com.zfer.kit.StrKit.java

/**
 * ??String.//w  w w .  ja v  a  2s. c om
 *
 * @param obj input param
 * @param dateFormat input param
 * @return return str by obj
 */
public static String getStrByObj(Object obj, String dateFormat) {
    String rs = "";

    if (obj == null) {
        return rs;
    }

    if (obj instanceof BigDecimal) {
        BigDecimal bigDecimal = (BigDecimal) obj;
        rs = bigDecimal.toString();
    } else if (obj instanceof java.sql.Date || obj instanceof java.sql.Time || obj instanceof java.sql.Timestamp
            || obj instanceof java.util.Date) {
        Date dateObj = (Date) DateKit.transDateObj2UtilDate(obj);
        rs = DateKit.toStr(dateObj, dateFormat);
    } else {
        rs = obj.toString();
    }

    return rs;
}

From source file:com.spaceprogram.simplejpa.util.AmazonSimpleDBUtil.java

public static String encodeRealNumberRange(BigDecimal number, int maxDigitsLeft, int maxDigitsRight,
        BigDecimal offsetValue) {
    BigDecimal shiftMultiplier = new BigDecimal(Math.pow(10, maxDigitsRight));
    //        System.out.println("shiftMultiplier=" + shiftMultiplier);
    BigDecimal shiftedNumber = number.multiply(shiftMultiplier);
    //        System.out.println("shiftedNumber=" + shiftedNumber);
    shiftedNumber = shiftedNumber.setScale(0, BigDecimal.ROUND_HALF_UP);
    //        System.out.println("shiftedNumber rounded=" + shiftedNumber);
    BigDecimal shiftedOffset = offsetValue.multiply(shiftMultiplier);
    //        System.out.println("shiftedOffset=" + shiftedOffset);
    BigDecimal offsetNumber = shiftedNumber.add(shiftedOffset);
    //        System.out.println("offsetNumber=" + offsetNumber);
    String longString = offsetNumber.toString();
    //        System.out.println("shifted string=" + longString);
    int numBeforeDecimal = longString.length();
    int numZeroes = maxDigitsLeft + maxDigitsRight - numBeforeDecimal;
    StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
    for (int i = 0; i < numZeroes; i++) {
        strBuffer.insert(i, '0');
    }/*from   w  ww .j a  va  2  s. c  o m*/
    strBuffer.append(longString);
    return strBuffer.toString();
}

From source file:raptor.chess.pgn.PgnUtils.java

/**
 * Chess base EMT format. 1. e4 {[%emt 0.0]} e6 {[%emt 0.0]} 2. Nc3 {[%emt
 * 1.398]} Nf6 {[%emt 0.1]}//  www  .  j  a  v a 2s  . c o  m
 */
public static String timeToEMTFormat(long elapsedTimeMillis) {
    double elapsedTimeInSeconds = elapsedTimeMillis / 1000.0;
    BigDecimal bigDecimal = new BigDecimal(elapsedTimeInSeconds);
    bigDecimal = bigDecimal.setScale(3, BigDecimal.ROUND_HALF_UP);
    return "[%emt " + bigDecimal.toString() + "]";
}

From source file:org.cryptomath.util.NumberUtil.java

public static String absorbFloats(String value) {
    String result;/*from www .  ja v  a2 s.  co  m*/
    if (value.matches("[\\d]*.[\\d]*")) {
        logger.info("Absorbing floats");
        BigDecimal bd = new BigDecimal(value);
        int f = CryptoConfigSpec.getInstance().getMathConfig().getFractions();
        //       logger.info("Absorbed value::check" + value);
        //       logger.info("Absorbed value::check" + bd);
        bd = bd.multiply(new BigDecimal("10").pow(f));
        //            logger.info("Absorbed value::check" + bd);
        String[] tokens = bd.toString().split("\\.");
        result = tokens[0];
        //       result = truncate(value, f).toString();
        logger.info("Absorbed value::check::" + result);
    } else {
        throw new NumberFormatException(MessageFormat.format("Invalid argument {0}", value));
    }
    return result;
}

From source file:org.openmrs.contrib.databaseexporter.util.Util.java

public static String toPercent(Number numerator, Number denominator, int decimals) {
    BigDecimal bd = new BigDecimal(100 * numerator.doubleValue() / denominator.doubleValue());
    bd = bd.setScale(decimals, BigDecimal.ROUND_HALF_UP);
    return bd.toString();
}

From source file:morphy.utils.MorphyStringUtils.java

/**
 * Returns the value specified in bytes into megs. Currently only shows 2
 * digits after the decimal and rounds half up.
 *//*from w  ww.  jav a2  s .c  o m*/
public static String getMegs(long bytes) {
    BigDecimal bigDecimal = new BigDecimal(bytes / 1048576.0);
    bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    return bigDecimal.toString() + "Megs";
}

From source file:org.kuali.kfs.gl.document.CorrectionDocumentUtils.java

/**
 * Converts the value into a string, with the appropriate formatting
 * /*from   ww  w  . j  a v a  2s.  c  om*/
 * @param fieldActualValue actual field value
 * @param fieldType field type (i.e. "String", "Integer", "Date")
 * @return String object value as a string
 */
public static String convertToString(Object fieldActualValue, String fieldType) {
    if (fieldActualValue == null) {
        return "";
    }
    if ("String".equals(fieldType)) {
        return (String) fieldActualValue;
    } else if ("Integer".equals(fieldType)) {
        Integer i = (Integer) fieldActualValue;
        return i.toString();
    } else if ("KualiDecimal".equals(fieldType)) {
        KualiDecimal kd = (KualiDecimal) fieldActualValue;
        return kd.toString();
    } else if ("BigDecimal".equals(fieldType)) {
        BigDecimal bd = (BigDecimal) fieldActualValue;
        return bd.toString();
    } else if ("Date".equals(fieldType)) {
        Date d = (Date) fieldActualValue;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(d);
    }
    return "";
}

From source file:Main.java

/**
 * Compute the natural logarithm of x to a given scale, x > 0.
 *//*from w  ww  .  j  a v  a 2  s  .c om*/
public static BigDecimal ln(BigDecimal x, int scale) {
    // Check that x > 0.
    if (x.signum() <= 0) {
        throw new IllegalArgumentException("x <= 0");
    }

    // The number of digits to the left of the decimal point.
    int magnitude = x.toString().length() - x.scale() - 1;

    if (magnitude < 3) {
        return lnNewton(x, scale);
    }

    // Compute magnitude*ln(x^(1/magnitude)).
    else {

        // x^(1/magnitude)
        BigDecimal root = intRoot(x, magnitude, scale);

        // ln(x^(1/magnitude))
        BigDecimal lnRoot = lnNewton(root, scale);

        // magnitude*ln(x^(1/magnitude))
        return BigDecimal.valueOf(magnitude).multiply(lnRoot).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
    }
}