Results 1 to 2 of 2
  1. #1
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default One Decimal Place

    Hello there... I have this code. I want to round the converted value to one decimal place. How can I do that? Thank you.. :)

    Java Code:
    public class darjahCelcius{
    	
    	public static void main(String[] args) {
    		
    
    	System.out.println("Celsius\t\tFahrenheit\t|\tFahrenheit\t\tCelsius");
    	
    	System.out.println("---------------------------------------------");
    	
    	double celsius = 40; double fahrenheit = 120;
    	
    	for (int i = 1; i <= 10; celsius--, fahrenheit -= 10, i++) {
    		System.out.println(celsius + "\t\t" +
    			celsiusToFahrenheit(celsius) + "\t\t|\t" + fahrenheit + "\t\t\t" +
    				fahrenheitToCelsius(fahrenheit));
    				
    					}
    				}
    				
    				public static double celsiusToFahrenheit(double celsius) {
    					return (9.0 / 5.0) * celsius + 32;
    					
    					}
    					
    					public static double fahrenheitToCelsius(double fahrenheit) {
    						return (5.0 / 9) * (fahrenheit - 32);
    						
    							}
    						
    						}

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: One Decimal Place

    First of all, your code looks horrible. I'm not saying it's bad (though not good either), only that it looks terrible. It's hard to read and follow your code without getting lost in what you have done. Look at other threads or Java code examples and learn how to indent and structure your code properly.

    To answer your question, look into the class DecimalFormat. Read about it in the Java API page: DecimalFormat (Java Platform SE 7 )

    Look into how you set up a DecimalFormat and then at the methods to apply it to your numbers. To accomplish what you want, you only need one new code line and two modifications of your code.

Similar Threads

  1. Output is off by very small decimal place
    By jester-race in forum New To Java
    Replies: 3
    Last Post: 02-14-2012, 10:08 AM
  2. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  3. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  4. I Like This Place
    By joshdgreen in forum Introductions
    Replies: 0
    Last Post: 10-12-2010, 11:07 AM
  5. Hopefully in the right place.
    By ilop12 in forum New To Java
    Replies: 2
    Last Post: 06-03-2010, 02:35 AM

Posting Permissions

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