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.

user914357
  • 15
  • 1
  • 5
  • There's a class for that! [http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html](http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html) – Will Feb 24 '12 at 13:40
  • have you googled? Have you looked decimatFormat? http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html – Nishant Feb 24 '12 at 13:41
  • How do you get the value, i.e. what Java type? – Hauke Ingmar Schmidt Feb 24 '12 at 13:41
  • http://stackoverflow.com/questions/264937/how-to-check-if-a-double-has-at-most-n-decimal-places and http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java should do it. – Anton Feb 24 '12 at 13:44

2 Answers2

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);
    }
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • What about inputs like 1.002? – jarnbjo Feb 24 '12 at 14:17
  • @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. – Gilbert Le Blanc Feb 24 '12 at 14:20
  • @: but i need output value as double not string.if i do Double.parseDouble(formatter1.format(input[i])) ,i am loosing decimal place. – user914357 Feb 24 '12 at 15:31
  • @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. – Gilbert Le Blanc Feb 24 '12 at 15:36
  • @my input value is string not double.After conversion i need double value with above mentioned format. – user914357 Feb 24 '12 at 15:43
  • @user914357: You use Double.parseDouble to convert an input String to a Double. While the value is a Double, you don't have to format it at all. You just use it in calculations. When you're ready to output the Double value, you convert it back to a String for display using the code I provided or something similar. – Gilbert Le Blanc Feb 24 '12 at 15:47
  • @Gilbert Le Blanc 4 : i tried similar way ,not able to get expected values.if the value 1.2 it dispaying 1.2 only not 1.20 – user914357 Feb 24 '12 at 15:53
  • The fix only works for locales using '.' as a decimal separator. – jarnbjo Feb 24 '12 at 16:24
-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

david a.
  • 5,283
  • 22
  • 24