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 5 of 5

Thread: How to make Math.round() round to the nearest .1?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default How to make Math.round() round to the nearest .1?

    I want round to round to the nearest 10th instead of the nearest integer... Any ideas?

    public class Triangle 
    {
        private final int leg;
        private final double hypotenuse;
     
        public Triangle( int l )
        {
            leg = l;
            hypotenuse = leg*Math.sqrt( 2.0 );
        }    
     
        public int getLeg()
        {
            return leg;
        }
     
        public double getHypotenuse()
        {
            return hypotenuse;
        }
     
        @Override
        public String toString()
        {
            return String.format( "Triangle(%d)", leg );
        }
    }

    Triangle(5)
    Leg: 5
    Hypotenuse: 7.0 (this should be 7.1)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to make Math.round() round to the nearest .1?

    One idea: *10, round, /10
    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:

    bdennin (February 2nd, 2013)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to make Math.round() round to the nearest .1?

    Do do you want to round the value hypotenuse - ie produce a completely different double value - or merely format the value so that it appears with one decimal place?

    I ask because people often confuse rounding (double->double) with formatting (double->String). And often imagine that double values have decimal places: in fact decimal places are a property of the String (base 10) representation of double values.

    Assuming you want to format you would use

    String.format("%.1f", hypotenuse);

    The "f" means you are formatting a floating point value (a double) and the ".1" means you want one decimal place to appear.

    Full details in the Formatter API docs. There is rather a lot to absorb there: too much to attempt to remember. But it's worth bookmarking and going back to whenever formatting problems arise.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    bdennin (February 2nd, 2013)

  6. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to make Math.round() round to the nearest .1?

    String.format("%.1f", hypotenuse);

    Ahhh, so easy. I can't believe I didn't think of that. Thank you, as always.

  7. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to make Math.round() round to the nearest .1?

    You're welcome.

Similar Threads

  1. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  2. Newbie all round
    By camillejb in forum Member Introductions
    Replies: 1
    Last Post: June 25th, 2012, 05:08 PM
  3. How can I make a JDialog to make main Frame to "sleep"
    By piulitza in forum AWT / Java Swing
    Replies: 1
    Last Post: May 11th, 2012, 08:00 AM
  4. take a double, then round decimal to either to .0 or .5 only
    By MaxBodine in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2011, 03:35 PM
  5. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM