fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. double[] doubles = { 12345.6789 , 1.23456789 , 0.123456789 } ;
  13.  
  14. for( double d : doubles )
  15. {
  16. String s = String.valueOf( d ) ;
  17. String s5 = s.substring( 0 , s.contains( "." ) ? 6 : 5 ) ;
  18. if( s5.endsWith( "." ) ) { s5 = s5.substring( 0 , s5.length() - 1 ) ; } // Drop FULL STOP in last place.
  19. System.out.println( s5 ) ;
  20. }
  21. }
  22. }
Success #stdin #stdout 0.08s 47496KB
stdin
Standard input is empty
stdout
12345
1.2345
0.1234