# Formatting this output to show "#.##"

• 02-06-2010, 09:37 AM
twiggy62
Formatting this output to show "#.##"
How can I get the output of this program to display:

Code:

```[COLOR="Blue"]public class[/COLOR] Test {     [COLOR="blue"]public static double[/COLOR] calculate([COLOR="blue"]final double[/COLOR] x, [COLOR="blue"]final double[/COLOR] y) {         [COLOR="blue"]class[/COLOR] Helper         {             [COLOR="blue"]double[/COLOR] doSomething()             {                 [COLOR="blue"]return[/COLOR] Math.sqrt(x) + Math.sqrt(y);             }         }         Helper h = [COLOR="Blue"]new[/COLOR] Helper();         [COLOR="blue"]return[/COLOR] Math.exp(h.doSomething());     }     [COLOR="blue"]public static void[/COLOR] main(String[] args)     {  System.[COLOR="SeaGreen"]out[/COLOR].println(calculate(4.0, 9.0));     } }```
• 02-06-2010, 12:37 PM
tim
Hi Twiggy

You can achieve truncation by multiplying, casting and dividing your number as follows:
Code:

```    public static void main(String[] args)     {         double answer = (double) ((int) (calculate(4.0, 9.0) * 100d)) / 100d;         System.out.println(answer);     }```
Check the Math.round() method as well. ;)

Goodluck
Tim
• 02-06-2010, 12:51 PM
JosAH
Quote:

Originally Posted by tim
Hi Twiggy

You can achieve rounding by multiplying, casting and dividing your number

Don't do that: use a Format object to do the job; the printf( ... ) method uses those objects for its format specifiers; e.g.

Code:

`System.out.printf("%.3f", 4.123456);`
kind regards,

Jos
• 02-06-2010, 01:10 PM
FlyNn
Quote:

Don't do that: use a Format object to do the job; the printf( ... ) method uses those objects for its format specifiers; e.g.

Code:
System.out.printf("%.3f", 4.123456);
kind regards,
I had similar problem, am I correct to think that the output will be 4.123?

Thanks
• 02-06-2010, 01:26 PM
JosAH
Quote:

Originally Posted by FlyNn
I had similar problem, am I correct to think that the output will be 4.123?

Thanks

Yep, but don't take my word for it but try it instead.

kind regards,

• 02-06-2010, 01:39 PM
FlyNn
Yeah that was my next step. I should of tried it before though. My mistake.
I am glad you like the signature, thanks.
• 02-06-2010, 04:04 PM
Fubarable
If you like printf (and I do), you can get the same result with String#format and also Formatter. The latter two are useful if you want the formatted number to appear in a non-console environment such as a Swing app.
• 02-06-2010, 05:57 PM
twiggy62
After reviewing both proposed solutions, the real answer for me is using a combination of both.

Code:

```public class Test {     public static double calculate(final double x, final double y) {         class Helper {             double doSomething() {                 return Math.sqrt(x) + Math.sqrt(y);             }         }         Helper h = new Helper();         return Math.exp(h.doSomething());     }     public static void main(String[] args) {         System.out.println(calculate(4.4, 9.2));         double answer = (double) ((int) (calculate(4.4, 9.2) * 1000d)) / 1000d;         System.out.println(answer);         System.out.printf("%.2f", answer);     } }```
Gives me the following results:

run:
169.1469517303579
169.146
169.15
BUILD SUCCESSFUL (total time: 1 second)

However to get these results, for the Format object, I have to multiply by 1,000 instead of 100, and if I wanted to round to nearest 100th, then I would have to multiply by 10,000, nearest 1000th multiply by 100,000, etc, etc.
My final solution would look like this:

Code:

```public class Test {     public static double calculate(final double x, final double y) {         class Helper {             double doSomething() {                 return Math.sqrt(x) + Math.sqrt(y);             }         }         Helper h = new Helper();         return Math.exp(h.doSomething());     }     public static void main(String[] args) {         double answer = (double) ((int) (calculate(4.0, 9.0) * 1000d)) / 1000d;         System.out.printf("%.2f", answer);     } }```
With these results:

run:
148.41
BUILD SUCCESSFUL (total time: 1 second)

Thanks for all contributions! :)
• 02-06-2010, 06:11 PM
JosAH
Quote:

Originally Posted by twiggy62
The first reply works best in my case, because I am not simply trying to format a number (using the Format object), I am also performing a calculation at the same time, so I don't know what the result is until the calculations are performed.

Code:

`System.out.printf("result: %.3f", <very_complicated_calculation>);`
?

kind regards,

Jos
• 02-06-2010, 06:40 PM
twiggy62
Oh, I see where you are coming from, you are right. I was just over thinking it.

My solution would then look like this:

Code:

```public class Test {     public static double calculate(final double x, final double y) {         class Helper {             double doSomething() {                 return Math.sqrt(x) + Math.sqrt(y);             }         }         Helper h = new Helper();         return Math.exp(h.doSomething());     }     public static void main(String[] args) {         System.out.printf("%.2", calculate(4.0, 9.0));     } }```