Results 1 to 4 of 4
Thread: Printf problem!!!
- 11-06-2010, 07:01 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
-
Please show us how you're trying to use printf.
Here's an example of using printf to specify precision of displayed double/float value: SpecifyingtheWidthandPrecisionLast edited by Fubarable; 11-06-2010 at 09:00 PM.
- 11-06-2010, 09:29 PM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
-
I'm going to disagree here as rounding is not going to solve what is essentially a display problem. e.g.,
Java Code:double foo = 1.37 * 3.5 * 3 * 2; foo /= (21); System.out.println("cost = " + foo); System.out.printf("cost = %.2f%n", foo);
Of course there is more than one way to skin this cat, and if in fact cost represents a display of currency, then why not use a NumberFormat.getCurrencyInstance?
Java Code:public static void main(String[] args) { double cost = 1.37 * 3.5 * 3 * 2; cost /= (21); System.out.println("cost = " + cost); System.out.printf("cost = %.2f%n", cost); NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); System.out.printf("cost = %s%n", currencyFormat.format(cost)); }Last edited by Fubarable; 11-06-2010 at 09:59 PM.
Similar Threads
-
Few errors with for loop/printf
By ks1615 in forum New To JavaReplies: 8Last Post: 03-12-2009, 09:47 PM -
Printf error
By ks1615 in forum New To JavaReplies: 4Last Post: 03-10-2009, 06:59 AM -
printf error with int and double
By o0n1 in forum New To JavaReplies: 1Last Post: 09-18-2008, 08:58 PM -
Demonstration of printf() method
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:37 PM -
printf
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks