Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Decimals going too far

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Decimals going too far

    I had to make a table showing different values for converting kg to lbs and I'm confused because when it compiles, some of the decimals show correctly and some of them show with several 0's added on with another number on the end. Please help, I've tried to do math.random, but I'm not sure how to tie it in with my program. Please help.
    //1 kg = 2.2 lbs
    public class kg_to_lbs {
    	public static void main(String[] args) {
    //not sure if values are needed..
    	int kg = 1;
    	double lbs = 2.20; 
     
    //Table title
    	System.out.println("   Kilograms to Pounds");
     
    //First line headings
    	System.out.println("  Kilograms   |   Pounds  ");
    //do-while loop	
    do {
    		System.out.println("      " + kg + "       |    " + (kg * lbs) + "  ");
    		kg++;
    	}
    while (kg <= 199);
    //end loop
    	}	
    }
    and this is some of the results I get
          1       |    2.2  
          2       |    4.4  
          3       |    6.6000000000000005  
          4       |    8.8  
          5       |    11.0  
          6       |    13.200000000000001


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Decimals going too far

    show with several 0's added on with another number on the end.
    There are many ways to get rid of that.

    One way
    Is to use DecimalFormat class.
    There are API that can help you with your error
    (see the link: http://docs.oracle.com/javase/7/docs...malFormat.html)

    Other way
    Convert/Cast your answer to float.
    it's either store your answer into float type variable,
    or if your variable is in double type variable, just cast it to float when printing:
    example:
    double answer = 3 * 2.20;
    System.out.println((float) answer);

    I think float data type has only one decimals.

    Other way
    you can also use substring. Just convert you decimal result to
    String and used substring, you can use the index of dot ( . )
    as reference on how many decimals you want to get.

    And there are other ways,

  3. The Following User Says Thank You to dicdic For This Useful Post:

    Elyril (February 19th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Decimals going too far

    I put converted to float, but sadly, I was unable to get the decimals all the way down to 1 decimal point. And the DecimalFormat class won't work for me, might've been the way I was putting it though. Thanks anyway!

  5. #4
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Decimals going too far

    Instead of println method to print your output, format your output using printf method like this,

    System.out.printf("%d  | %.2f\n",kg,kg*lbs);

  6. The Following User Says Thank You to aprabhat For This Useful Post:

    Elyril (February 20th, 2014)

Similar Threads

  1. Can't put decimals in my textfields?
    By Kevin2341 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2013, 06:38 AM
  2. NumberFormat class, rounding decimals (Beginner needs help!)
    By richyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 9th, 2012, 09:31 AM
  3. Replies: 1
    Last Post: August 29th, 2011, 07:32 PM
  4. [SOLVED] Processing Decimals <1 & >0
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 24th, 2011, 01:53 PM
  5. Convert Decimals to Octals using only If/Else
    By jcattau in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 9th, 2011, 12:58 PM