# Thread: How to print decimals.

1. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## How to print decimals.

Hi guys, I am making a small program that converts meters to feet.
I want my answer to be printed with 2 decimals, but I am struggelig.
This is my current code, any takes on what needs to be changed and why?

Thanks for the help!:)

import java.util.Scanner;
import java.text.DecimalFormat;

public class Chapter2PE3 {
public static void main (String[]args){

Scanner input = new Scanner (System.in);

System.out.println("Type in feet");
double feet = input.nextDouble();

//Calculate
double meters = feet * 0.305;

DecimalFormat df = new DecimalFormat ("#,##");
System.out.println(feet + " feet is " + df.format(meters) + "meters");

}
}

2. ## Re: How to print decimals.

What you should be passing to DecimalFormat when it is created is "#.00" That should fix your problem.

3. ## Re: How to print decimals.

Alternatively
Java Code:
`System.out.println( String.format( "%.2f feet is %.2f meters", feet, meters ) );`

4. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

Originally Posted by jbarke12
What you should be passing to DecimalFormat when it is created is "#.00" That should fix your problem.
Thank you for you reply, if I change: DecimalFormat df = new DecimalFormat ("#,##"); to DecimalFormat df = new DecimalFormat ("#.00");

It prints the answer I am looking for great! Thank you very much! What is the reason for when to use ,## or .00 ?

5. ## Re: How to print decimals.

According to the API documentation, the # indicates an optional decimal, while 0 always prints a digit (even if it equals zero).

kind regards,

Jos

6. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

Originally Posted by JosAH
According to the API documentation, the # indicates an optional decimal, while 0 always prints a digit (even if it equals zero).

kind regards,

Jos
Great! Thank you.

If I want the decimals to round to the closes 0.50? Can I still use "DecimalFormat" or will I need to do something different?
This is something that will be very helpful when making programs where the output is in dollars.

Thanks for all the help! :)

7. ## Re: How to print decimals.

DecimalFormat doesn't round for you. Math Round rounds to the nearest whole number. I don't know of an API that rounds to the nearest half. Its probably a method you would have to create.

8. ## Re: How to print decimals.

Math.round(value - 0.5) + 0.5 would do what you want

9. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

Originally Posted by jbarke12
Math.round(value - 0.5) + 0.5 would do what you want
Great that works perfectly.
I guess it works like this : 11.76-0.50 = 11.26, gets rounded to 11 and then you add back on the 0.50?

10. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: How to print decimals.

So, even if you have a number like 11.99 you want to round it to 11.50?

Regards,
Jim

11. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

Originally Posted by jbarke12
Math.round(value - 0.5) + 0.5 would do what you want
Great that works perfectly.
I guess it works like this : 11.76-0.50 = 11.26, gets rounded to 11 and then you add back on the 0.50?
Then it will end up rounding to 11.50 even tho the correct answer is 12.00

12. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

No, I want to round it to the closest 0.50 , 1.0 , 1.50 and so on.
So 11.99 I would like to round to 12.00.

"Great that works perfectly.
I guess it works like this : 11.76-0.50 = 11.26, gets rounded to 11 and then you add back on the 0.50?"

Will actually not work, since in the situation above, it will round to 11.50 even tho the closes 0.5 increment is 12.00.

Any suggestions?

13. ## Re: How to print decimals.

You can make a BigDecimal round 'half even', 'half up' or 'half down'.

kind regards,

Jos

14. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

Originally Posted by JosAH
You can make a BigDecimal round 'half even', 'half up' or 'half down'.

kind regards,
Thank you JosAH, I have never used BigDecimal in my code before, how will I use it my code?
If this is the line I want to have rounded: System.out.println(total);
i.e the double total is the variable I want to round to the closes 0.5 increment.

15. ## Re: How to print decimals.

Have you read the API documentation for the BigDecimal class?

kind regards,

Jos

16. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

Originally Posted by JosAH
Have you read the API documentation for the BigDecimal class?

kind regards,

Jos
I have not, I have never used BigDecimal before in my coding.

17. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

18. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: How to print decimals.

Originally Posted by Tolls
Thank you! :)
Thanks for all the help guys!

#### Posting Permissions

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