Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 27th, 2012  |  syntax: None  |  size: 0.58 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Formatting double to 2 decimals doesn't work
  2. 3.499999999999999
  3.        
  4. 3.50
  5.        
  6. DecimalFormat df = new DecimalFormat("0.00");
  7.         double result = Double.valueOf(df.format(input));
  8.         System.out.println(answer);
  9.        
  10. public double round(double input)
  11.     {
  12.         int decimalPlace = 2;
  13.         BigDecimal bd = new BigDecimal(input);
  14.         bd = bd.setScale(decimalPlace,BigDecimal.ROUND_UP);
  15.  
  16.         return (bd.doubleValue());
  17.     }
  18.        
  19. 3.5
  20.        
  21. DecimalFormat df = new DecimalFormat("0.00");
  22.     System.out.println(df.format(input));
  23.        
  24. System.out.printf("%.2f%n", 3.5);
  25.        
  26. 3.50