tags:

views:

123

answers:

6

suppose if the value is 200.3456 it should be formatted to 200.34 or if it is 200 then it should be 200.00

+3  A: 

Rounding a double is usually not what one wants. Instead, use String.format() to represent it in the desired format.

Ignacio Vazquez-Abrams
+1  A: 

Do you really want 200.34? Or what you want is 200.35? If the latter is what you want, it is called truncating, not rounding.

monn
Truncating is one of a number of possible rounding modes. The common "round half up" is another one.
Michael Borgwardt
A: 

The easiest way, would be to do a trick like this;

double val = ....;
val = val*100;
val = Math.Round(val);
val = val /100;

if val starts at 200.3456 then it goes to 20034.56 then it gets rounded to 20035 then we divide it to get 200.34.

if you wanted to always round down we could always truncate by casting to an int:

double val = ....;
val = val*100;
val = (double)((int) val);
val = val /100;

This technique will work for most cases because for very large doubles (positive or negative) it may overflow. but if you know that your values will be in an appropriate range then this should work for you.

luke
+2  A: 

If you just want to print a double with two digits after the decimal point, use something like this:

double value = 200.3456;
System.out.printf("Value: %.2f", value);

If you want to have the result in a String instead of being printed to the console, use String.format() with the same arguments:

String result = String.format("%.2f", value);

Or use class DecimalFormat:

DecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));
Jesper
And with `DecimalFormat` you can also select the rounding mode; default will match the OP's example or they may want `RoundingMode.DOWN`.
Kevin Brock
Good point, Kevin.
Jesper
A: 

in your question it seems that you want to avoid rounding the numbers as well? i think .format() will round the numbers using half-up, afaik?
so if you want rounding, 200.3456 should be 200.35 for a precision of 2. but in your case, if you just want the first 2 and then discard the rest... ??:

you could multiply it by 100 and then cast to an int (or taking the floor of the number), before dividing by 100 again.

200.3456 * 100 = 20034.56;
(int) 20034.56 = 20034;
20034/100.0 = 200.34;

you might have issues with really really big numbers close to the boundary though. in which case converting to a string and substring'ing it would work just as easily.

bryanallott
thanks for your answer. It is working fine if the value is 200.3456, but if the value is 200, then it should be 200.00
Rajesh
+1  A: 

Here's an utility that rounds (instead of truncating) a double to specified number of decimal places.

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
}

Thus, for example:

round(200.3456, 2); // returns 200.35

If you wanted String formatting instead of (or in addition to) strictly rounding numbers, see the other answers.

Specifically, note that round(200, 0) returns 200.0. If you want to output "200.00", you should first round and then format the result for output (which is perfectly explained in Jesper's answer).

Jonik
Once again, downvotes are more useful with a comment. (Note that the question *is* ambiguous and my answer makes it clear that it doesn't tackle every interpretation of the question.)
Jonik