1

How to number formating with Java in various scenarios:

  1. if the value is 0,then output will be zero.
  2. if the value 1,then output will be 1.
  3. if the value is 1.2,then output will be 1.20.
  4. if the value is 1.20,then output will be 1.20.

So it means if the input value has decimals then i have to apply numberformat to two decimal places otherwise not required.

asked Feb 24, 2012 at 13:38
4

2 Answers 2

3

One DecimalFormatter isn't going to work for the case of no decimal and the case of two decimal places.

Here's some code that meets all 4 of your conditions:

 DecimalFormat formatter1 = new DecimalFormat("0");
 DecimalFormat formatter2 = new DecimalFormat("0.00");
 double[] input = {0, 1, 1.2, 1.265};
 for (int i = 0; i < input.length; i++) {
 double test = Math.round(input[i]);
 if (Math.abs(test - input[i]) < 1E-6) {
 System.out.println(formatter1.format(input[i]));
 } else {
 System.out.println(formatter2.format(input[i]));
 }
 }

Edited to add: For jambjo, a version that manipulates the String after the DecimalFormatter.

 DecimalFormat formatter2 = new DecimalFormat("0.00");
 double[] input = {0, 1, 1.2, 1.265};
 for (int i = 0; i < input.length; i++) {
 String result = formatter2.format(input[i]);
 int pos = result.indexOf(".00");
 if (pos >= 0) {
 result = result.substring(0, pos);
 }
 System.out.println(result);
 }
answered Feb 24, 2012 at 14:02
Sign up to request clarification or add additional context in comments.

8 Comments

What about inputs like 1.002?
@jarnbjo: An input of 1.002 would display as 1.00. It's up to user914357 if that's acceptable or not. I assumed user914357 was dealing with money with at most 2 decimal places.
@: but i need output value as double not string.if i do Double.parseDouble(formatter1.format(input[i])) ,i am loosing decimal place.
@user914357: Why do you need the output value as a double? You already have the input value. Your question is about the display of a double, which is usually done with a String.
@my input value is string not double.After conversion i need double value with above mentioned format.
|
-2

See http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html . An example of such format would be:

double input = 1.2;
DecimalFormat formatter = new DecimalFormat("0.00");
String result = formatter.format(input);

Decimal symbol (as well as other symbols used in the result string) will be dependent on system locale (unless set otherwise) - see http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html#setDecimalFormatSymbols%28java.text.DecimalFormatSymbols

answered Feb 24, 2012 at 13:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.