1. Member Join Date
Oct 2010
Posts
10
Rep Power
0 Printf problem!!!

Hi.İ want to format with print f for double cost=1.37*10
it is output 1.3700001 ı want to 1.37 show help me pls  Reply With Quote

2. 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: SpecifyingtheWidthandPrecision
Last edited by Fubarable; 11-06-2010 at 09:00 PM.  Reply With Quote

3. Senior Member Join Date
Oct 2010
Posts
139
Rep Power
0  Originally Posted by pinar Hi.İ want to format with print f for double cost=1.37*10
it is output 1.3700001 ı want to 1.37 show help me pls
Hi I had the exact problem and I used this code to fix the problem:
Java Code:
double resS0 = resS%1;//resS is the number you want rounded
double resS1 = resS0 * 100;
double resS2 = Math.round(resS1);
double resS3 = resS2 / 100;
good luck!  Reply With Quote

4.  Originally Posted by Johanis Hi I had the exact problem and I used this code to fix the problem:
Java Code:
double resS0 = resS%1;//resS is the number you want rounded
double resS1 = resS0 * 100;
double resS2 = Math.round(resS1);
double resS3 = resS2 / 100;
good luck!

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.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•