private static final String ProbabilityToString(double val,int significantDigits){
  if (val == 0) {
    return "0.0";
  }
  String valString=Double.toString(val);
  int valStringLen=valString.length();
  String result="";
  int next=0;
  int significantDigitsFound=0;
  while ((next < valStringLen) && (valString.charAt(next) == '0')) {
    next++;
  }
  while ((next < valStringLen) && Character.isDigit(valString.charAt(next))) {
    result=result + valString.charAt(next);
    significantDigitsFound++;
    next++;
  }
  if (next == valStringLen) {
    return result;
  }
 else   if (valString.charAt(next) != '.') {
    return valString;
  }
  if (significantDigitsFound >= significantDigits) {
    next++;
    while ((next < valStringLen) && Character.isDigit(valString.charAt(next))) {
      next++;
    }
  }
 else {
    next++;
    result=result + ".";
    if (significantDigitsFound == 0) {
      while ((next < valStringLen) && (valString.charAt(next) == '0')) {
        next++;
        result=result + "0";
      }
    }
    while ((next < valStringLen) && Character.isDigit(valString.charAt(next)) && significantDigitsFound < significantDigits) {
      result=result + valString.charAt(next);
      next++;
      significantDigitsFound++;
    }
    if ((next < valStringLen) && Character.isDigit(valString.charAt(next)) && Character.digit(valString.charAt(next),10) >= 5) {
      int prev=result.length() - 1;
      boolean done=false;
      while (!done) {
        if (prev < 0) {
          result="1" + result;
          done=true;
        }
 else {
          char prevChar=result.charAt(prev);
          String front=result.substring(0,prev);
          String back=result.substring(prev + 1);
          if (Character.isDigit(prevChar)) {
            if (prevChar == '9') {
              result=front + '0' + back;
            }
 else {
              result=front + Character.forDigit(Character.digit(prevChar,10) + 1,10) + back;
              done=true;
            }
          }
 else {
          }
        }
        prev--;
      }
    }
    while ((next < valStringLen) && Character.isDigit(valString.charAt(next))) {
      next++;
    }
  }
  if (next >= valStringLen) {
    return result;
  }
  if (valString.charAt(next) == 'E') {
    next++;
    result=result + "E";
    while (next < valStringLen) {
      result=result + valString.charAt(next);
      next++;
    }
    return result;
  }
  return valString;
}

private static final String ProbabilityToString ( double val , int significantDigits ) { if ( val == 0 ) { return "0.0" ; } String valString = Double . toString ( val ) ; int valStringLen = valString . length ( ) ; String result = "" ; int next = 0 ; int significantDigitsFound = 0 ; while ( ( next < valStringLen ) && ( valString . charAt ( next ) == '0' ) ) { next ++ ; } while ( ( next < valStringLen ) && Character . isDigit ( valString . charAt ( next ) ) ) { result = result + valString . charAt ( next ) ; significantDigitsFound ++ ; next ++ ; } if ( next == valStringLen ) { return result ; } else if ( valString . charAt ( next ) != '.' ) { return valString ; } if ( significantDigitsFound >= significantDigits ) { next ++ ; while ( ( next < valStringLen ) && Character . isDigit ( valString . charAt ( next ) ) ) { next ++ ; } } else { next ++ ; result = result + "." ; if ( significantDigitsFound == 0 ) { while ( ( next < valStringLen ) && ( valString . charAt ( next ) == '0' ) ) { next ++ ; result = result + "0" ; } } while ( ( next < valStringLen ) && Character . isDigit ( valString . charAt ( next ) ) && significantDigitsFound < significantDigits ) { result = result + valString . charAt ( next ) ; next ++ ; significantDigitsFound ++ ; } if ( ( next < valStringLen ) && Character . isDigit ( valString . charAt ( next ) ) && Character . digit ( valString . charAt ( next ) , 10 ) >= 5 ) { int prev = result . length ( ) - 1 ; boolean done = false ; while ( ! done ) { if ( prev < 0 ) { result = "1" + result ; done = true ; } else { char prevChar = result . charAt ( prev ) ; String front = result . substring ( 0 , prev ) ; String back = result . substring ( prev + 1 ) ; if ( Character . isDigit ( prevChar ) ) { if ( prevChar == '9' ) { result = front + '0' + back ; } else { result = front + Character . forDigit ( Character . digit ( prevChar , 10 ) + 1 , 10 ) + back ; done = true ; } } else { } } prev -- ; } } while ( ( next < valStringLen ) && Character . isDigit ( valString . charAt ( next ) ) ) { next ++ ; } } if ( next >= valStringLen ) { return result ; } if ( valString . charAt ( next ) == 'E' ) { next ++ ; result = result + "E" ; while ( next < valStringLen ) { result = result + valString . charAt ( next ) ; next ++ ; } return result ; } return valString ; }
this method added by ll on 17 april 2012 to replace the use of the printfformat method in reportsuccess . returns a string representing the decimal representation of a probability to a given number of significant digits . if the input is not a probability , or if some error is found , then it returns the result of applying double . tostring ( long ) to the value . warning : the code makes the following assumption : - double . tostring ( v ) returns a decimal representation of v of the form [ d ] * [ " . " [ d ] + [ " e " [ + | - ] [ d ] + ] where d is a decimal digit and [ x ] = 0 or 1 instance of x [ x ] * = any number of instances of x [ x ] + = any non - zero number of instances of x x | y = an x or a y
