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

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

Description

double To String

License

Open Source License

Declaration

public static String doubleToString(double value, int afterDecimalPoint) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    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//  w w w  .  j a  v a  2s . c om
                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 doubleToString(double value, int width, int afterDecimalPoint) {
        String tempString = doubleToString(value, afterDecimalPoint);
        char[] result;
        int dotPosition;

        if ((afterDecimalPoint >= width) || (tempString.indexOf('E') != -1))
            return tempString;

        result = new char[width];
        for (int i = 0; i < result.length; i++)
            result[i] = ' ';

        if (afterDecimalPoint > 0) {
            dotPosition = tempString.indexOf('.');
            if (dotPosition == -1)
                dotPosition = tempString.length();
            else
                result[width - afterDecimalPoint - 1] = '.';
        } else
            dotPosition = tempString.length();

        int offset = width - afterDecimalPoint - dotPosition;
        if (afterDecimalPoint > 0)
            offset--;

        if (offset < 0)
            return tempString;

        for (int i = 0; i < dotPosition; i++)
            result[offset + i] = tempString.charAt(i);

        for (int i = dotPosition + 1; i < tempString.length(); i++)
            result[offset + i] = tempString.charAt(i);

        return new String(result);
    }
}

Related

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