-1

I have reviewed a number of threads related to formatting numbers. For the most part it works except for the value 3.101. Float and Double have been used along with the ## and 00 format.

import java.text.DecimalFormat;

public class DecimalFormatTest {
public static void main(String[] args) 
{
    Float d1 = -3.1011f;
    Double ds = -3.1011;
    DecimalFormat df = new DecimalFormat("#.##");
    System.out.println (Double.valueOf(df.format(d1)));
    System.out.println (Double.valueOf(df.format(ds)));

    DecimalFormat df2 = new DecimalFormat("#.00");
    System.out.println (Double.valueOf(df2.format(d1)));
    System.out.println (Double.valueOf(df2.format(ds)));
 }
}

The output is :

-3.1
-3.1
-3.1
-3.1

Expected output :

-3.10
-3.10
-3.10
-3.10

As mentioned above, this works for all the other numbers I have tested. For some reason this is causing an issue.

Any ideas as to what makes this number so different and what extra step is needed to get the 2nd digit?

Keep in mind, the key is that I want to eventually return a Float or Double.

Unhandled Exception
  • 1,427
  • 14
  • 30
  • Possible duplicate of [How do I round a double to two decimal places in Java?](http://stackoverflow.com/questions/5710394/how-do-i-round-a-double-to-two-decimal-places-in-java) – Omore Apr 07 '17 at 20:21
  • I'm not entirely clear what your question is. – Joe C Apr 07 '17 at 20:24
  • 1
    Remove the `Double.valueOf` bits and it will work better. – assylias Apr 07 '17 at 20:26
  • 1
    You are formatting the value correctly, but then re-parsing it as a double (which will shave off the trailing zeros). – jtahlborn Apr 07 '17 at 20:27
  • I removed the Double.valueOf and it works using the "#.00" format, but I do need to return a Double and not the string. – Unhandled Exception Apr 07 '17 at 20:41
  • Doubles do not track trailing zeros or precision information. If you want to track those things, doubles are not the right data type to use. (Also, `Double` is a wrapper type, which you shouldn't use unless you specifically need an object instead of a primitive for some reason. Spell it `double` to get the primitive type.) – user2357112 Apr 07 '17 at 21:09

3 Answers3

3

Try this.

public static void main(String[] args) 
    {
        Float d1 = -3.1011f;
        Double ds = -3.1011;
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println (df.format(d1));
        System.out.println (df.format(ds));

        DecimalFormat df2 = new DecimalFormat("0.00");
        System.out.println (df2.format(d1));
        System.out.println (df2.format(ds));
     }
Omore
  • 614
  • 6
  • 18
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Apr 07 '17 at 20:28
0

This works:

Double number = 3.101;
System.out.printf("%.2f", number);
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Apr 07 '17 at 20:28
0

The main culprit here is Double.valueOf . Try removing it and it should work fine like below: Also, putting ("#.##") wil cause further problem . So rather use any ("0.00") at-least after precision.

import java.text.DecimalFormat;

public class DecimalFormatTest {
public static void main(String[] args) 
    {
        Float d1 = -3.1011f;
        Double ds = -3.1011;
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println (df.format(d1));
        System.out.println (df.format(ds));

        DecimalFormat df2 = new DecimalFormat("0.00");
        System.out.println (df2.format(d1));
        System.out.println (df2.format(ds));
     }
}

For reference : Double decimal formatting in Java

Community
  • 1
  • 1
dildeepak
  • 1,349
  • 2
  • 16
  • 34