0

I am trying to round the following value 1.48824E9, that is returning inside the date.getTime() method.

This is what I've tried but It's not working:

    private double x;
    private double y;
    DecimalFormat df;
    private double z;
    private String g;
    double a;

    public GraphPoints(Date x, double y) {
        df = new DecimalFormat("#.#");
        df.setRoundingMode(RoundingMode.CEILING);
        a = x.getTime();
        g = df.format(a);
        z = Double.parseDouble(g);
        System.out.println("THIS IS THE ROUNDED VALUE: " + z);
        this.x = z;

        this.x = z;
        this.y = y;
    }

I'm trying to round it to one decimal place. Could someone help me please?

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48
jimmy neutron
  • 11
  • 1
  • 5

1 Answers1

0

You don't have any decimal places in your number (I mean you have 2 decimal places by default .00, but I assume that it is not what you are looking for). "1.48824E9" is a scientific notation for the "1488240000.00" double. Consider this code snippet:

Double d = Double.parseDouble("1.48824E9");
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(d));

System.out.println(d);

Output:

1488240000.00
1.48824E9

Not sure though how you get it because date.getTime() returns long and Long.toString() doesn't convert to scientific notation. So, if you want to print "1.5" from "1.48824E9" you can do it like this for example:

Double d = Double.parseDouble("1.48824E9");
d = d / Math.pow(10, 9);
NumberFormat formatter = new DecimalFormat("#0.0");

System.out.println(formatter.format(d));

Output:

1.5

If you actually want to change the value of your double you can do it this way:

Double d = Double.parseDouble("1.48824E9");
d = d / Math.pow(10, 9);
d = Math.round (d * 10.0) / 10.0; 

System.out.println(d);

Output:

1.5
Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
  • Hi @Sergei Sirik, this is great, however, when attempting to do this, i get the following value when i rounded it: 1490.9. The value that I'm trying to round is first converted into a string, and then parsed into a double and the rest follows through, but im not getting 1.5. – jimmy neutron Jan 06 '18 at 12:50