So I want to use the Decimal Format class to round numbers:
double value = 10.555;
DecimalFormat fmt = new DecimalFormat ("0.##");
System.out.println(fmt.format(value));
Here, the variable value
would be rounded to 2 decimal places, because there are two #
s. However, I want to round value
to an unknown amount of decimal places, indicated by a separate integer called numPlaces
. Is there a way I could accomplish this by using the Decimal Formatter?
e.g. If numPlaces = 3
and value = 10.555
, value
needs to be rounded to 3 decimal places
DecimalFormat
? Because it's much easier if you useFormatter
instead. – Retrograde5
. You already accepted mine, so I don't see why you would unaccept it. Mine's more well-written anyways and was first. – Teets#
s). The only difference between his answer and mine originally was he took my code out of the method. – Teets