-7

I have a double such as 32.559.

How would I get rid of all but one decimal digit so my output would look like 32.5?

Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Josh.O
  • 27
  • 4
  • See java.lang.Math.round and java.text.DecimalFormat – ControlAltDel Sep 01 '15 at 14:15
  • 3
    The lack of own research will probably lead to a lot downvotes ... You should add what you have tried and why it did not meet your requirements. – Fildor Sep 01 '15 at 14:19
  • Please at least make some research effort next time. A google search with your *exact* title copy and pasted reveals this obvious duplicate: [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – tnw Sep 01 '15 at 14:27

3 Answers3

0
float f = 32.559f;
System.out.printf("%.1f", f);
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
0

I'm assuming you just want to print it that way:

String result = String.format("%.1f", value);
DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

You can use DecimalFormat:

Locale locale  = new Locale("en", "UK");
String pattern = "###.#";

DecimalFormat decimalFormat = (DecimalFormat)
NumberFormat.getNumberInstance(locale);
decimalFormat.applyPattern(pattern);

String format = decimalFormat.format(32.559);
System.out.println(format);

The output printed from this code would be:

32.5

This is the Basic above, you can go throws here for more details.

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
Janny
  • 681
  • 1
  • 8
  • 33