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.

Page 2 of 2 FirstFirst 12
Results 26 to 28 of 28

Thread: Formatting answers to 2 decimal places

  1. #26
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Formatting answers to 2 decimal places

    That's kind of what I was hoping you could tell me.

    It's set up so that the user has an option of choosing a material from a drop-down menu (materialDropDown). Then they choose a shape--cylinder or rectangle--from another one (shapeDropDown). Then they enter either the diameter & length, or the length, width, & height depending on the shape (separate coding for shapeDropDown pre-fills the unneeded textfields and locks them from being edited). Then they hit the 'calculate' button, within which all the above code is contained, and it's supposed to return the price. Then a 'clear' button returns all fields and menus to defaults.

    The GUI works to a 'T'. The calculations work 100% for the cylinder, but not for the rectangular portion unless the cylinder section is not evaluated, as discussed. It has to be something flawed in the 'if' statements themselves, don't you think?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formatting answers to 2 decimal places

    It has to be something flawed in the 'if' statements themselves, don't you think?
    It's possible. Go through that section of the code very slowly and carefully in the debugger.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Cal S. (September 11th, 2014)

  4. #28
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Formatting answers to 2 decimal places

    That's alot of work so far for working on formatting decimals into 2 spaces.

    What I would do is...

    for a number example: 52.33950258

    		double amount = 52.22950158;
    		String s = ""+amount+"";
    		String beforeDecimal = s.split("\\.")[0];
    		String afterDecimal = s.split("\\.")[1].substring(0,2);
    		s = beforeDecimal+"."+afterDecimal;		
    		System.out.println(s);

    Result: 52.22

    If you want to round, do

    String 3rddigit = s.split("\\.")[1].substring(2,3);
     
    if(integer.parseint(3rddigit) > 5){
    afterdecimal = afterdecimal.substring(0,1)+(integer.parseint(afterdecimal.substring(1,2)+1));
    }

    To be honest, if you want to get a more accurate decimal round, I average each decimal space after the 2 you show as a result.

    If those spaces don't average more than 5, I wouldn't round it. But that's quite a bit of code. You could condense it in a for loop though.
    Last edited by micecd; September 13th, 2014 at 03:24 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How do I get my answers to out with 2 decimal places?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 8th, 2012, 12:23 PM
  2. New to Java Need 2 decimal places, please :). Here is my code
    By Charyl in forum Member Introductions
    Replies: 2
    Last Post: June 28th, 2011, 03:06 AM
  3. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  4. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM