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);
}

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 ) ; }
rounds a double and converts it into a formatted decimal - justified string . trailing 0 ' s are replaced with spaces .
