Results 1 to 10 of 10
- 02-06-2010, 08:37 AM #1
Formatting this output to show "#.##"
How can I get the output of this program to display:
148.41 instead of 148.4131591025766
Java 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, 11:37 AM #2
Hi Twiggy
You can achieve truncation by multiplying, casting and dividing your number as follows:
Check the Math.round() method as well. ;)Java Code:public static void main(String[] args) { double answer = (double) ((int) (calculate(4.0, 9.0) * 100d)) / 100d; System.out.println(answer); }
Goodluck
TimLast edited by tim; 02-07-2010 at 02:07 PM. Reason: Casting from double precision to integer precision causes truncation.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 02-06-2010, 11:51 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 02-06-2010, 12:10 PM #4
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
I had similar problem, am I correct to think that the output will be 4.123?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,
ThanksMeasuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 02-06-2010, 12:26 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 02-06-2010, 12:39 PM #6
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Yeah that was my next step. I should of tried it before though. My mistake.
I am glad you like the signature, thanks.Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
-
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, 04:57 PM #8
After reviewing both proposed solutions, the real answer for me is using a combination of both.
Gives me the following results:Java 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); } }
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:
With these results:Java 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); } }
run:
148.41
BUILD SUCCESSFUL (total time: 1 second)
Thanks for all contributions! :)Last edited by twiggy62; 02-06-2010 at 05:31 PM.
- 02-06-2010, 05:11 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 02-06-2010, 05:40 PM #10
Oh, I see where you are coming from, you are right. I was just over thinking it.
My solution would then look like this:
Java 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)); } }
Similar Threads
-
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
how to override "cancel operation" in "progress bar"
By singswt in forum SWT / JFaceReplies: 2Last Post: 10-08-2009, 11:28 PM -
output "left click"/enter command?
By Arsenic in forum New To JavaReplies: 2Last Post: 06-04-2009, 05:01 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks