@Milhous: the decimal format for rounding is excellent:
Quote: You can also use the
DecimalFormat df = new DecimalFormat("#.00000");
df.format(0.912385);
to make sure you have the trailing 0's.
You can also use the
DecimalFormat df = new DecimalFormat("#.00000"); df.format(0.912385);to make sure you have the trailing 0's.
I would add that, this method is very good at providing an actual
numeric numeric, rounding mechanism - not only visually, but also when processing.
Hypothetical: you have to implement a rounding mechanism into a GUI
program program. To alter the accuracy / precision of a resultantresult output simply
change change the caret format (i.e. within the brackets). So that:
DecimalFormat df = new DecimalFormat("#0.######"); df.format(0.912385);
DecimalFormat df = new DecimalFormat("#0.######");
df.format(0.912385);
would return as output: 0.9123850.912385
DecimalFormat df = new DecimalFormat("#0.#####"); df.format(0.912385);
DecimalFormat df = new DecimalFormat("#0.#####");
df.format(0.912385);
would return as output: 0.912390.91239
DecimalFormat df = new DecimalFormat("#0.####"); df.format(0.912385);
DecimalFormat df = new DecimalFormat("#0.####");
df.format(0.912385);
would return as output: 0.91240.9124
[EDIT: also if the caret format is like so ("#0.############") and you
enter enter a decimal, e.g. 3.1415926, for argument's sake, DecimalFormat
does does not produce any garbage (e.g. trailing zeroes) and will return:
3.1415926
3.1415926 .. if you're that way inclined. Granted, it's a little verbose
for for the liking of some dev's - but hey, it's got a low memory footprint
during during processing and is very easy to implement.] So essentially, the
So essentially, the beauty of DecimalFormat is that it simultaneously handles the string
appearance appearance - as well as the level of rounding precision set. Ergo: you
get get two benefits for the price of one code implementation. ;)