-3

I have an array of numbers that have many decimal formats. How do I take that array and round everything to 2 decimal places? Ex 10.52

Chris
  • 246
  • 1
  • 5
  • 15

2 Answers2

0

DecimalFormat df = new DecimalFormat("#.##"); df.format(10.52777);

Pulkit Sethi
  • 1,325
  • 1
  • 14
  • 22
0

trying using BigDecimal http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html

When using you can then set the scale to 2 or whatever you want. If however you just want to display as 2 digits then you can use System.out.printf("%.2f", val); or DecimalFormat df = new DecimalFormat("#.##");

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • I am not needing to print them out right now. I just need to take my array of numbers and cut them into 2 decimal places. The decimal format wouldnt work then. – Chris Sep 17 '13 at 01:17
  • so, as per the first part of my answer, use BigDecimal - perfect for rounding – Scary Wombat Sep 17 '13 at 01:25