# Java help. trying to get my double variable to show up as .500 instead of .5?

I'm and creating a program and it is supposed to show the double variables rounded to the thousandth position ex. 4.25 would show up as 4.250 or .75 would show up as .750.

please help. I have tried a couple of different things but they haven't worked

please help. I have tried a couple of different things but they haven't worked

Update:
This is what I have now.

x[K] = Math.round((x[K])*1000.0)/1000.0;

y[K] = Math.round((y[K])*1000.0)/1000.0;

x[K] = Math.round((x[K])*1000.0)/1000.0;

y[K] = Math.round((y[K])*1000.0)/1000.0;

Update 2:
This is in a loop so I need all of the outputs to be the same length (to the thousandth position) I won't be able to edit each individually as they come out because the output varies depending on the user input

Update 3:
I have never done formatting before and I tried out what you gave me but it's giving me an error message.
for (int K = 1; K < numberX; K++) {
x [K] = (x [K-1] + increment);
y [K] = (20 * Math.abs(Math.sin(x[K])));
x[K] = Math.round((x[K])*1000.0)/1000.0;
y[K] =...
show more
I have never done formatting before and I tried out what you gave me but it's giving me an error message.

for (int K = 1; K < numberX; K++) {

x [K] = (x [K-1] + increment);

y [K] = (20 * Math.abs(Math.sin(x[K])));

x[K] = Math.round((x[K])*1000.0)/1000.0;

y[K] = Math.round((y[K])*1000.0)/1000.0;

System.out.printf("%.3f\n", "x: " + x[K] + ", " + "y: " + y[K]);

}

for (int K = 1; K < numberX; K++) {

x [K] = (x [K-1] + increment);

y [K] = (20 * Math.abs(Math.sin(x[K])));

x[K] = Math.round((x[K])*1000.0)/1000.0;

y[K] = Math.round((y[K])*1000.0)/1000.0;

System.out.printf("%.3f\n", "x: " + x[K] + ", " + "y: " + y[K]);

}

Follow

3 answers
3

Are you sure that you want to delete this answer?