Round a double up to the next Integer [duplicate] - java

I can't seem to find the answer I'm looking for regarding a simple question: how do I round up any number to the nearest int?
For example, whenever the number is 0.2, 0.7, 0.2222, 0.4324, 0.99999 I would want the outcome to be 1.
So far I have
int b = (int) Math.ceil(a / 100);
It doesn't seem to be doing the job, though.

Math.ceil() is the correct function to call. I'm guessing a is an int, which would make a / 100 perform integer arithmetic. Try Math.ceil(a / 100.0) instead.
int a = 142;
System.out.println(a / 100);
System.out.println(Math.ceil(a / 100));
System.out.println(a / 100.0);
System.out.println(Math.ceil(a / 100.0));
System.out.println((int) Math.ceil(a / 100.0));
Outputs:
1
1.0
1.42
2.0
2
See http://ideone.com/yhT0l

I don't know why you are dividing by 100 but here my assumption int a;
int b = (int) Math.ceil( ((double)a) / 100);
or
int b = (int) Math.ceil( a / 100.0);

int RoundedUp = (int) Math.ceil(RandomReal);
This seemed to do the perfect job. Worked everytime.

10 years later but that problem still caught me.
So this is the answer to those that are too late as me.
This does not work
int b = (int) Math.ceil(a / 100);
Cause the result a / 100 turns out to be an integer and it's rounded so Math.ceil
can't do anything about it.
You have to avoid the rounded operation with this
int b = (int) Math.ceil((float) a / 100);
Now it works.

Just another option. Use basics of math:
Math.ceil(p / K) is same as ((p-1) // K) + 1

The easiest way to do this is just:
You will receive a float or double and want it to convert it to the closest round up then just do System.out.println((int)Math.ceil(yourfloat));
it'll work perfectly

Assuming a as double and we need a rounded number with no decimal place . Use Math.round() function.
This goes as my solution .
double a = 0.99999;
int rounded_a = (int)Math.round(a);
System.out.println("a:"+rounded_a );
Output :
a:1

Related

Rounding down a float number to 2 decimal points in java

I have a variable x that is 11.885, when I round this to 2 decimal points I want it to give me 11.88 but it won't work and I can't figure it out. I won't be using import or anything but just can't figure out a way to round this number down.
What I originally used:
double x = 11.885
double y = (double) Math.round(x * 100.0) / 100.0;
but this gives me 11.89. I also need this without printing it since I will be using this rounded number in my code.
Instead of Math.round(double), use Math.floor(double) like
double x = 11.885;
double y = Math.floor(x * 100) / 100.0;
System.out.println(y);
I get (as requested)
11.88
Try BigDecimal It has a lot of helpful features like this.
double x = 11.885;
BigDecimal y = BigDecimal.valueOf(x);
y.setScale(2, BigDecimal.ROUND_HALF_UP)

Rounding of very large floats in Java

Java:
float big = (float) 1e12;
float ulp = Math.ulp(big);
float result = (big + 2/3*ulp) - big;
result is 0.0, while I was expecting ulp (65536.0). Can somebody explain why?
This is because 2/3 = 0 (integer division), try 2.0 / 3
You may try this:
float result = (big + 2f/3*ulp) - big;
ie, you need to typecast the division values else integer/integer will result to zero.
Try typecasting either 2 or 3 to float and try, as 2/3=0, thats why result=0.0

Java percent of number [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
Is there any way to calculate (for example) 50% of 120?
I tried:
int k = (int)(120 / 100)*50;
But it doesn't work.
int k = (int)(120 / 100)*50;
The above does not work because you are performing an
integer division expression (120 / 100) which result is
integer 1, and then multiplying that result to 50, giving
the final result of 50.
If you want to calculate 50% of 120, use:
int k = (int)(120*(50.0f/100.0f));
more generally:
int k = (int)(value*(percentage/100.0f));
int k = (int)(120*50.0/100.0);
Never use floating point primitive types if you want exact numbers and consistent results, instead use BigDecimal.
The problem with your code is that result of (120/100) is 1, since 120/100=1.2 in reality, but as per java, int/int is always an int.
To solve your question for now, cast either value to a float or double and cast result back to int.
I suggest using BigDecimal, rather than float or double. Division by 100 is always exact in BigDecimal, but can cause rounding error in float or double.
That means that, for example, using BigDecimal 50% of x plus 30% of x plus 20% of x will always sum to exactly x.
it is simple as 2 * 2 = 4 :)
int k = (int)(50 * 120) / 100;
Division must be float, not int
(120f * 50 / 100f)
You don't need floating point in this case you can write
int i = 120 * 50 / 100;
or
int i = 120 / 2;

Math.round and Math.ceil not working

I'm trying to round the users input but I can seem to get my double to round to an int. Basically, when I enter 4.4999 it wont round up to 5.
Any ideas?
Math.ceil() returns the ceiled value. It can't change the value of the variable it takes as argument, because Java passes arguments by value. So you need to do
hours = Math.ceil(hours);
The actual solution is to use double inside the ceil method.
Math.ceil(7 * 50 / 100) will return 3.0 even though the actual value resulting from 7*50/100 is 3.5. It is because since everything is int, the result of 350/100 itself will be 3.
If however, if you give Math.ceil(7 * 50 / 100D), the result would be 4.0.
So, the 4.999 in your question should be a double and not a result of an integer operation like 4999/1000.
Just make sure that whatever you give inside a the ceil is double and not an int.
Both function return the rounded (or ceiled) values, but don't change the variable passed as parameter.
Use eg. hours = Math.ceil(hours);.
Math.ciel returns a Double. Something like this should work (inside of your hours > 0 block):
cost += Math.ceil(hours) * hourlyRate;
You're not assigning the result of Math.ceil(hours) back to hours so it will never round.
int a = 15
int b = 2;
int x = (int) Math.ceil( a / b );
int y = (int) Math.ceil( (double) a / (double) b );
Results:
x: 7
y: 8

Why is my int 0? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does int exp1 = 14/20*100; equals ‘0’ in java?
I am using this:
int newWidth = Math.round(297 * (40 / 414));
In the code the numbers are actually variables, but those are what they were holding, and when I run the code newWidth comes back as 0, and I was expecting 28 or 29. I don't see what is wrong here...
40 / 414 is rounded to 0 immediately as it only works with integers. You must either cast one of the operands to double/float or use double immediately:
int newWidth = (int)Math.round(297 * (40.0 / 414));
or:
int newWidth = (int)Math.round(297 * ((double)40 / 414));
Because 40/414 equals to 0, so 297*0 = 0 and Math.round(0) = 0
Use doubles instead.
int newWidth = (int)Math.round(297 * (40d / 414));
Change to int newWidth = (int)Math.round(297 * (40.0 / 414));
An int in java uses no decimal places... Therefore you should use a double for this. It is giving you a 0 because inside the () 40/ 414 = 0.
The problem starts with 40/414: that's integer division, which returns an int. In this case: 0.
To fix it, cast one of those ints to a double or float, e.g. ((float) 40 / 414).
The result of 40/414 is zero. This is a consequence of integer division. Change either numerator or denominator to a floating point value to get your desired result
Gonna throw my 2 cents in... as noted, the issue lies in the integer division. Another method for forcing this division to treat the result as a double:
int newWidth = Math.round(297 * (40.0 / 414));
40/414 is rounded to 0.
What you probably want is
int newWidth = (297 * 40) / 414;

Categories

Resources