fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.*;
  7.  
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. System.out.println("Assuming US Locale: ',' as thousand separator, '.' as decimal separator)");
  15.  
  16. NumberFormat nf = new DecimalFormat("#,##0.0##");
  17. System.out.println("\n==============================");
  18. System.out.println("With Format (#,##0.0##) ");
  19. System.out.println("------------------------------");
  20. System.out.println("1234.0 = " + nf.format(1234.0));
  21. System.out.println("123.4 = " + nf.format(123.4));
  22. System.out.println("12.34 = " + nf.format(12.34));
  23. System.out.println("1.234 = " + nf.format(1.234));
  24. System.out.println("==============================");
  25.  
  26. nf = new DecimalFormat("#,000.000");
  27. System.out.println("\n==============================");
  28. System.out.println("With Format (#,000.000) ");
  29. System.out.println("------------------------------");
  30. System.out.println("1234.0 = " + nf.format(1234.0));
  31. System.out.println("123.4 = " + nf.format(123.4));
  32. System.out.println("12.34 = " + nf.format(12.34));
  33. System.out.println("1.234 = " + nf.format(1.234));
  34. System.out.println("==============================");
  35. }
  36. }
Success #stdin #stdout 0.09s 381696KB
stdin
Standard input is empty
stdout
Assuming US Locale: ',' as thousand separator, '.' as decimal separator)

==============================
With Format (#,##0.0##) 
------------------------------
1234.0 = 1,234.0
123.4  = 123.4
12.34  = 12.34
1.234  = 1.234
==============================

==============================
With Format (#,000.000) 
------------------------------
1234.0 = 1,234.000
123.4  = 123.400
12.34  = 012.340
1.234  = 001.234
==============================