Java Double to String doubleToString(double value, int afterDecimalPoint)

Here you can find the source of doubleToString(double value, int afterDecimalPoint)

Description

Rounds a double and converts it into String.

License

Apache License

Parameter

Parameter Description
value the double value
afterDecimalPoint the (maximum) number of digits permitted after the decimal point

Return

the double as a formatted string

Declaration

public static String doubleToString(double value, int afterDecimalPoint) 

Method Source Code

//package com.java2s;
/**/*from w  ww .j a  va  2  s  .  co m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *     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.
 */

public class Main {
    /**
     * Rounds a double and converts it into String.
     * 
     * @param value the double value
     * @param afterDecimalPoint the (maximum) number of digits permitted after the
     *          decimal point
     * @return the double as a formatted string
     */
    public static String doubleToString(double value, int afterDecimalPoint) {

        StringBuffer stringBuffer;
        double temp;
        int dotPosition;
        long precisionValue;

        temp = value * Math.pow(10.0, afterDecimalPoint);
        if (Math.abs(temp) < Long.MAX_VALUE) {
            precisionValue = (temp > 0) ? (long) (temp + 0.5) : -(long) (Math.abs(temp) + 0.5);
            if (precisionValue == 0) {
                stringBuffer = new StringBuffer(String.valueOf(0));
            } else {
                stringBuffer = new StringBuffer(String.valueOf(precisionValue));
            }
            if (afterDecimalPoint == 0) {
                return stringBuffer.toString();
            }
            dotPosition = stringBuffer.length() - afterDecimalPoint;
            while (((precisionValue < 0) && (dotPosition < 1)) || (dotPosition < 0)) {
                if (precisionValue < 0) {
                    stringBuffer.insert(1, '0');
                } else {
                    stringBuffer.insert(0, '0');
                }
                dotPosition++;
            }
            stringBuffer.insert(dotPosition, '.');
            if ((precisionValue < 0) && (stringBuffer.charAt(1) == '.')) {
                stringBuffer.insert(1, '0');
            } else if (stringBuffer.charAt(0) == '.') {
                stringBuffer.insert(0, '0');
            }
            int currentPos = stringBuffer.length() - 1;
            while ((currentPos > dotPosition) && (stringBuffer.charAt(currentPos) == '0')) {
                stringBuffer.setCharAt(currentPos--, ' ');
            }
            if (stringBuffer.charAt(currentPos) == '.') {
                stringBuffer.setCharAt(currentPos, ' ');
            }

            return stringBuffer.toString().trim();
        }
        return new String("" + value);
    }

    public static String toString(byte[] data, int offset) {
        int length = toShort(data, offset);
        offset += 2;
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = (char) data[offset];
            offset++;
        }
        return new String(chars);
    }

    public static short toShort(byte[] b, int offset) {
        return (short) (((b[offset] & 0x000000FF) << 8) + ((b[offset + 1] & 0x000000FF)));
    }
}

Related

  1. doubleToString(double d, int sigDigs)
  2. doubleToString(Double doub)
  3. doubleToString(double inValue, int precision, boolean useComma)
  4. doubleToString(double val)
  5. doubleToString(double val, int digits)
  6. doubleToString(double value, int afterDecimalPoint)
  7. doubleToString(double[] values)
  8. doubleToString(final double d)
  9. doubleToString(final double v, final int roundingDigits)