public static int toDecimalDigits ( double v , char [ ] digits , int offset , int maxDigits , int maxFractDigits , int roundMode ) { // NOTE: The value 144115188075855872 is converted to // 144115188075855870, which is as correct as possible. Java's // Double.toString method doesn't round off the last digit. long bits = Double . doubleToLongBits ( v ) ; long f = bits & 0xfffffffffffff L ; int e = ( int ) ( ( bits >> 52 ) & 0x7ff ) ; if ( e != 0 ) { // Normalized number. return toDecimalDigits ( f + 0x10000000000000 L , e - 1022 , 53 , digits , offset , maxDigits , maxFractDigits , roundMode ) ; } else { // Denormalized number. return toDecimalDigits ( f , - 1023 , 52 , digits , offset , maxDigits , maxFractDigits , roundMode ) ; } }
Produces decimal digits for non - zero finite floating point values . The sign of the value is discarded . Passing in Infinity or NaN produces invalid digits . The maximum number of decimal digits that this function will likely produce is 18 .