469,327 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

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.
Feb 14 '08 #1
13 14858
Laharl
849 Expert 512MB
java.math.RoundingMode has what you're looking for, likely to be used in conjunction with java.math.BigDecimal.

Sun's Java API has all the methods you'll need.
Feb 14 '08 #2
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.
Feb 14 '08 #3
BigDaddyLH
1,216 Expert 1GB
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.
Feb 14 '08 #4
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.
Feb 14 '08 #5
BigDaddyLH
1,216 Expert 1GB
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.10000000000000001.

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.
Feb 14 '08 #6
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. :(
Feb 15 '08 #7
I'm pretty sure the ceiling() method is my answer. Any hints? :)
Feb 15 '08 #8
Laharl
849 Expert 512MB
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().
Feb 15 '08 #9
BigDaddyLH
1,216 Expert 1GB
I'm pretty sure the ceiling() method is my answer. Any hints? :)
Why not try it and see?
Feb 15 '08 #10
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.ceil(f1);
int i=(int)f2;
System.out.println(i);
}
}
Feb 15 '08 #11
BigDaddyLH
1,216 Expert 1GB
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.ceil(f1);
int i=(int)f2;
System.out.println(i);
}
}
I was hoping the original poster would figure that out for himself.
Feb 15 '08 #12
I did indeed figure it out by testing. :)


System.out.println("Lastly, enter Number of Hours.");
numberOfHours = Math.ceil(in.nextDouble());
intHours = (int)numberOfHours;

Because we had to accept a double, I converted the ceiling value to an int.
Feb 16 '08 #13
@BigDaddyLH
You really could use some people skills.
Aug 29 '13 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Norvin Laudon | last post: by
9 posts views Thread by itportal | last post: by
2 posts views Thread by whojustdenyme | last post: by
29 posts views Thread by Marco | last post: by
13 posts views Thread by Shirsoft | last post: by
206 posts views Thread by md | last post: by
20 posts views Thread by jacob navia | last post: by
reply views Thread by Gabriel Genellina | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.