# Printf problem!!!

• 11-06-2010, 08:01 PM
pinar
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
• 11-06-2010, 09:57 PM
Fubarable
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
• 11-06-2010, 10:29 PM
Johanis
Quote:

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:
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!
• 11-06-2010, 10:44 PM
Fubarable
Quote:

Originally Posted by Johanis
Hi I had the exact problem and I used this code to fix the problem:
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.,

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?

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));   }```