question: 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 ; }
target:  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