Java BigDecimal exponentialFormatBigDecimal(BigDecimal bd)

Here you can find the source of exponentialFormatBigDecimal(BigDecimal bd)

Description

Formats the given BigDecimal value into a floating-point literal (like we find in SQL).

License

Open Source License

Parameter

Parameter Description
bd The number to format.

Return

The formatted String.

Declaration

public static String exponentialFormatBigDecimal(BigDecimal bd) 

Method Source Code

//package com.java2s;
/**********************************************************************
Copyright (c) 2003 Andy Jefferson and others. All rights reserved. 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at/*  w  w  w. j av  a  2  s.c  o  m*/
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
     
    
Contributors:
2003 Erik Bengtson - moved replaceAll from Column class to here
2004 Andy Jefferson - moved intArrayToString, booleanArrayToString from SM
2007 Xuan Baldauf - toJVMIDString hex fix
...
**********************************************************************/

import java.math.BigDecimal;

public class Main {
    /**
     * Formats the given BigDecimal value into a floating-point literal (like we find in SQL).
     * BigDecimal.toString() is not well suited to this purpose because it never uses E-notation, 
     * which causes some values with large exponents to be output as long strings with tons of zeroes 
     * in them.
     * @param bd  The number to format.
     * @return  The formatted String.
     */
    public static String exponentialFormatBigDecimal(BigDecimal bd) {
        String digits = bd.unscaledValue().abs().toString();
        int scale = bd.scale();
        int len = digits.length();

        /* Normalize by removing any trailing zeroes. */
        while (len > 1 && digits.charAt(len - 1) == '0') {
            --scale;
            --len;
        }

        if (len < digits.length()) {
            digits = digits.substring(0, len);
        }

        StringBuilder sb = new StringBuilder();
        if (bd.signum() < 0) {
            sb.append('-');
        }

        int exponent = len - scale;
        if (exponent < 0 || exponent > len) {
            /* Output in E-notation. */
            sb.append('.').append(digits).append('E').append(exponent);
        } else if (exponent == len) {
            /* Output as an integer. */
            sb.append(digits);
        } else {
            /* Output as "intDigits.fracDigits". */
            sb.append(digits.substring(0, exponent)).append('.').append(digits.substring(exponent));
        }

        return sb.toString();
    }
}

Related

  1. currencyFormat(BigDecimal value, char decimalDelimiter)
  2. decimalDigits(BigDecimal d)
  3. deserializeBigDecimalFromString(String decimal)
  4. exp(BigDecimal power)
  5. exp(BigDecimal x, int scale)
  6. expTaylor(BigDecimal x, int scale)
  7. extractBigDecimal(String value)
  8. firstNonZero(final BigDecimal... values)
  9. floatValue(BigDecimal val)