473,394 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 15125
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

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

Similar topics

3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
9
by: itportal | last post by:
Hello, This will probably be a simple question. How can I round float/doubles to a specific digit after the comma/dot? For example we have float i = 1.234567. I want to round it to 1.234....
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
2
by: whojustdenyme | last post by:
Hey guys, I'm new to C and programming..so I need some help. I need a way to round an INPUT number to an INPUT value so for ex: enter a decimal: 3.1459 enter the number to round: 3 Answer:...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: Gabriel Genellina | last post by:
En Wed, 30 Jul 2008 00:56:55 -0300, <jyoung79@kc.rr.comescribi�: If all you need is to *display* the value - use formatting expressions like %f, the locale variants, the Template class, or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.