Skip to main content
  1. About
  2. For Teams
formatting the text - please do not use a paragraphs after every half sentence.
Source Link
Paŭlo Ebermann
  • 75k
  • 20
  • 150
  • 217

@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. ;)

@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.


I would add that, this method is very good at providing an actual

numeric, rounding mechanism - not only visually, but also when processing.

Hypothetical: you have to implement a rounding mechanism into a GUI

program. To alter the accuracy / precision of a resultant output simply

change the caret format (i.e. within the brackets). So that:

DecimalFormat df = new DecimalFormat("#0.######"); df.format(0.912385);

would return as output: 0.912385

DecimalFormat df = new DecimalFormat("#0.#####"); df.format(0.912385);

would return as output: 0.91239

DecimalFormat df = new DecimalFormat("#0.####"); df.format(0.912385);

would return as output: 0.9124

[EDIT: also if the caret format is like so ("#0.############") and you

enter a decimal, e.g. 3.1415926, for argument's sake, DecimalFormat

does not produce any garbage (e.g. trailing zeroes) and will return:

3.1415926 .. if you're that way inclined. Granted, it's a little verbose

for the liking of some dev's - but hey, it's got a low memory footprint

during processing and is very easy to implement.] So essentially, the

beauty of DecimalFormat is that it simultaneously handles the string

appearance - as well as the level of rounding precision set. Ergo: you

get two benefits for the price of one code implementation. ;)

@Milhous: the decimal format for rounding is excellent:

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, rounding mechanism - not only visually, but also when processing.

Hypothetical: you have to implement a rounding mechanism into a GUI program. To alter the accuracy / precision of a result output simply change the caret format (i.e. within the brackets). So that:

DecimalFormat df = new DecimalFormat("#0.######");
df.format(0.912385);

would return as output: 0.912385

DecimalFormat df = new DecimalFormat("#0.#####");
df.format(0.912385);

would return as output: 0.91239

DecimalFormat df = new DecimalFormat("#0.####");
df.format(0.912385);

would return as output: 0.9124

[EDIT: also if the caret format is like so ("#0.############") and you enter a decimal, e.g. 3.1415926, for argument's sake, DecimalFormat does not produce any garbage (e.g. trailing zeroes) and will return: 3.1415926 .. if you're that way inclined. Granted, it's a little verbose for the liking of some dev's - but hey, it's got a low memory footprint during processing and is very easy to implement.]

So essentially, the beauty of DecimalFormat is that it simultaneously handles the string appearance - as well as the level of rounding precision set. Ergo: you get two benefits for the price of one code implementation. ;)

added 448 characters in body
Source Link
Ivan
  • 311
  • 3
  • 3

@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.


I would add that, this method is very good at providing an actual

numeric, rounding mechanism - not only visually, but also when processing.

Hypothetical: you have to implement a rounding mechanism into a GUI

program. To alter the accuracy / precision of a resultant output simply

change the caret format (i.e. within the brackets). So that:

DecimalFormat df = new DecimalFormat("#0.######"); df.format(0.912385);

would return as output: 0.912385

DecimalFormat df = new DecimalFormat("#0.#####"); df.format(0.912385);

would return as output: 0.91239

DecimalFormat df = new DecimalFormat("#0.####"); df.format(0.912385);

would return as output: 0.9124

The[EDIT: also if the caret format is like so ("#0.############") and you

enter a decimal, e.g. 3.1415926, for argument's sake, DecimalFormat

does not produce any garbage (e.g. trailing zeroes) and will return:

3.1415926 .. if you're that way inclined. Granted, it's a little verbose

for the liking of some dev's - but hey, it's got a low memory footprint

during processing and is very easy to implement.] So essentially, the

beauty of DecimalFormat is that it simultaneously handles the string

the string appearance - as well as the level of rounding precision set.

  Ergo: you 

get two benefits for the price of one code implementation. ;)

@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.


I would add that, this method is very good at providing an actual

numeric, rounding mechanism - not only visually, but also when processing.

Hypothetical: you have to implement a rounding mechanism into a GUI

program. To alter the accuracy / precision of a resultant output simply

change the caret format (i.e. within the brackets). So that:

DecimalFormat df = new DecimalFormat("#0.######"); df.format(0.912385);

would return as output: 0.912385

DecimalFormat df = new DecimalFormat("#0.#####"); df.format(0.912385);

would return as output: 0.91239

DecimalFormat df = new DecimalFormat("#0.####"); df.format(0.912385);

would return as output: 0.9124

The beauty of DecimalFormat is that it simultaneously handles

the string appearance - as well as the level of rounding precision.

  Ergo: you get two benefits for the price of one implementation. ;)

@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.


I would add that, this method is very good at providing an actual

numeric, rounding mechanism - not only visually, but also when processing.

Hypothetical: you have to implement a rounding mechanism into a GUI

program. To alter the accuracy / precision of a resultant output simply

change the caret format (i.e. within the brackets). So that:

DecimalFormat df = new DecimalFormat("#0.######"); df.format(0.912385);

would return as output: 0.912385

DecimalFormat df = new DecimalFormat("#0.#####"); df.format(0.912385);

would return as output: 0.91239

DecimalFormat df = new DecimalFormat("#0.####"); df.format(0.912385);

would return as output: 0.9124

[EDIT: also if the caret format is like so ("#0.############") and you

enter a decimal, e.g. 3.1415926, for argument's sake, DecimalFormat

does not produce any garbage (e.g. trailing zeroes) and will return:

3.1415926 .. if you're that way inclined. Granted, it's a little verbose

for the liking of some dev's - but hey, it's got a low memory footprint

during processing and is very easy to implement.] So essentially, the

beauty of DecimalFormat is that it simultaneously handles the string

appearance - as well as the level of rounding precision set. Ergo: you 

get two benefits for the price of one code implementation. ;)

Source Link
Ivan
  • 311
  • 3
  • 3

@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.


I would add that, this method is very good at providing an actual

numeric, rounding mechanism - not only visually, but also when processing.

Hypothetical: you have to implement a rounding mechanism into a GUI

program. To alter the accuracy / precision of a resultant output simply

change the caret format (i.e. within the brackets). So that:

DecimalFormat df = new DecimalFormat("#0.######"); df.format(0.912385);

would return as output: 0.912385

DecimalFormat df = new DecimalFormat("#0.#####"); df.format(0.912385);

would return as output: 0.91239

DecimalFormat df = new DecimalFormat("#0.####"); df.format(0.912385);

would return as output: 0.9124

The beauty of DecimalFormat is that it simultaneously handles

the string appearance - as well as the level of rounding precision.

Ergo: you get two benefits for the price of one implementation. ;)

Morty Proxy This is a proxified and sanitized view of the page, visit original site.