Rounding up a float value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mygolfcircle
    New Member
    • Feb 2008
    • 8

    Rounding up a float value

    Hi everyone,

    I'd like some help to round UP a float value to the nearest higher integer.
    This means,

    1.2 will be rounded up to 2
    2.15 will be rounded up to 3
    4.01 will be rounded up to 5
    9.47 will be rounded up to 10 etc etc

    I think the functions to use lie somewhere in the math class, but i'm not sure how to manipulate the methods available therein.

    Please help :) Thank you.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    java.math.Round ingMode has what you're looking for, likely to be used in conjunction with java.math.BigDe cimal.

    Sun's Java API has all the methods you'll need.

    Comment

    • mygolfcircle
      New Member
      • Feb 2008
      • 8

      #3
      Hi there!

      Thanks for the reply, however, could anyone be kind enough to provide the actual code to complete the following :

      float x;
      x = 3.1417;

      //syntax to round float x upwards to 4
      x = ......

      Thanks fellas.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        BigDecimal is overkill. Take a look at the methods of java.lang.Math. If you read the documentation, you won't need to ask for others to write your code for you.

        Comment

        • mygolfcircle
          New Member
          • Feb 2008
          • 8

          #5
          I'd like to believe that the java.math API could actually assist, and have been experimenting with the nextUp() and round() methods. However, each time i try to use one of these, i get a method - class not found error in my script.

          i HAVE imported java.math.* previously at the start of the code.

          What are the probable causes of method - class not found? I am on 1.6 by the way.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by mygolfcircle
            I'd like to believe that the java.math API could actually assist, and have been experimenting with the nextUp() and round() methods. However, each time i try to use one of these, i get a method - class not found error in my script.

            i HAVE imported java.math.* previously at the start of the code.

            What are the probable causes of method - class not found? I am on 1.6 by the way.
            Are you confusing class java.lang.Math with package java.math? Note that, like class String, class Math is in package java.lang, which is automatically imported, so there is no need to import anything explicitly.

            As for nextUp, are you sure you want to use it? The nextUp of 3.1 is something like 3.1000000000000 0001.

            And do you want to use round? The round of 3.14 is 3 not 4 -- I thought you didn't want to get a smaller number as a result.

            Comment

            • mygolfcircle
              New Member
              • Feb 2008
              • 8

              #7
              I'm sorry. I have indeed confused the class with the package.

              However, i am still unsure how to find the nearest higher integer when a float is
              passed into the function.

              I'm still new to java, so i'll have to re-look the methods. :(

              Comment

              • mygolfcircle
                New Member
                • Feb 2008
                • 8

                #8
                I'm pretty sure the ceiling() method is my answer. Any hints? :)

                Comment

                • Laharl
                  Recognized Expert Contributor
                  • Sep 2007
                  • 849

                  #9
                  Math.ceil() would indeed do the job...I feel kinda silly for not thinking of that earlier. All you'd need to do is set an integer equal to the return value of ceil().

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by mygolfcircle
                    I'm pretty sure the ceiling() method is my answer. Any hints? :)
                    Why not try it and see?

                    Comment

                    • lakkireddyaruna
                      New Member
                      • Feb 2008
                      • 1

                      #11
                      Code for obtaining the ceiling value for a floating pointing number in int format

                      public class Ceil{
                      public static void main(String[] args){
                      float f1=12.3f;
                      float f2= (float)Math.cei l(f1);
                      int i=(int)f2;
                      System.out.prin tln(i);
                      }
                      }

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #12
                        Originally posted by lakkireddyaruna
                        Code for obtaining the ceiling value for a floating pointing number in int format

                        public class Ceil{
                        public static void main(String[] args){
                        float f1=12.3f;
                        float f2= (float)Math.cei l(f1);
                        int i=(int)f2;
                        System.out.prin tln(i);
                        }
                        }
                        I was hoping the original poster would figure that out for himself.

                        Comment

                        • mygolfcircle
                          New Member
                          • Feb 2008
                          • 8

                          #13
                          I did indeed figure it out by testing. :)


                          System.out.prin tln("Lastly, enter Number of Hours.");
                          numberOfHours = Math.ceil(in.ne xtDouble());
                          intHours = (int)numberOfHo urs;

                          Because we had to accept a double, I converted the ceiling value to an int.

                          Comment

                          • tpreston
                            New Member
                            • Aug 2013
                            • 1

                            #14
                            Originally posted by BigDaddyLH
                            BigDecimal is overkill. Take a look at the methods of java.lang.Math. If you read the documentation, you won't need to ask for others to write your code for you.
                            You really could use some people skills.

                            Comment

                            Working...