Math.round and Math.ceil not working - java

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

Related

Casting from Java primitive int to double

If I call
methodName(5, 1/2);
and it has the signature
public static int methodName(int x, double y){
}
does the methodName receive y with a value of 0 or 0.5?
int y = 1/2;
At this point, y is 0. If you try to cast it to a double afterwards it will be 0.0. It doesn't remember how it got its value, just what its value is.
EDIT: I think the compiler will actually replace 1/2 with 0 at compile time. Making the code literally identical to int y = 0
int y = 1/2;
In this code, y will be 0;
If you want to get it as 0.5
Have a try with the following code:
double y = 1.0 * 1 /2; //y is 0.5
It will evaluate to 0.
There's not a whole lot you can do with the above code.
There should be no specific reason to store y as an int.
Try this instead:
double y = 1/2.0;
int y = 1/2
Since you're using 1 (int) and 2 (int) to make the division, it's an integer division, thus y = 0 (and remainder (%) is 1).
I think you are confused with parameters (the parenthesis). In java every method has a set of parameters (they might not hold values ex: exampleMethod()).
A parameter is a variable that is passed into the method, so when in your code you call:
methodName() initialize it with methodName(x,y);
the x and y inside the method are just pointers for the values you pass through the parameters. I would suggest that you name your variables differently to avoid this confusion. For example:
int x;
int y;
methodName(int argX, double argY)
{
}
Also to answer your question, an int cuts off its stored value at the decimal point, so 5.9 would round to 5 rather than 6, so if you needed a floating point variable for y, either declare it as a float or a double, either will work, but most methods in the java library are written to take doubles as parameters rather than floats

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;

Java: Issue in converting result of exponential values to int

Suppose I have a range from 0-100 & I choose a random number X say 29.
Now I increase the range to a big number say a 10-digit 1023654740.
Now I want to find the place of that X in 0-1023654740. (so that is I belive is 29% of 1023654740)
If I perform the calculation using Double, I'm getting an exponent value.
double range = 1023654740;
double position = 29; // Can be any number from 0 - range
Double val = (((double) position / range) * 100);
Result: 2.8329864422842414E-6
But I want final result in int.(dont care if the final value is rounded off or truncated)
Any suggestions
First, your calculation is wrong. According to your description, you probably want to do the following:
Double val = ((double)position / 100) * range;
Then you can get your int value (by truncation) through:
int intVal = val.intValue();
as if your result is Double why you just dont do
val.intValue()
If it is a matter of presenting the result you should have a look at:
String myBeautifulInteger = NumberFormat.getIntegerInstance().format(val);
If it is just a matter of having an integer.
int myInt = val.intValue();

Why does int exp1 = 14/20*100; equals '0' in java?

I'm trying to do some basic math and it keeps popping up as 0. I'm sure it has to do with it being an int but I don't know how to work around it. I need to use integers but the math to arrive at those integers uses decimals. How do I do it?
That's integer division.
To get non-integer results, use doubles instead.
This is not special to blackberry, it's standard java behaviour.
This is because you're doing integer math:
int subexpr1 = 14 / 20; // 0
int subexpr2 = subexpr1 * 100; // 0
Use a double instead or change the order
int expr1 = (int) 14.0/20 * 100; // Very small possibility of rounding errors
int expr2 = 14 * 100 / 20; // Will ignore fraction parts
You can change it to 14*100/20 - and then it will give what you want.
I.e. change the sequence of operations (14/20 is 0)
Your result is being cast as an int, so you are losing precision.
Try
double exp1 = 14/20.0*100;

Converting double to integer in Java

In Java, I want to convert a double to an integer, I know if you do this:
double x = 1.5;
int y = (int)x;
you get y=1. If you do this:
int y = (int)Math.round(x);
You'll likely get 2. However, I am wondering: since double representations of integers sometimes look like 1.9999999998 or something, is there a possibility that casting a double created via Math.round() will still result in a truncated down number, rather than the rounded number we are looking for (i.e.: 1 instead of 2 in the code as represented) ?
(and yes, I do mean it as such: Is there any value for x, where y will show a result that is a truncated rather than a rounded representation of x?)
If so: Is there a better way to make a double into a rounded int without running the risk of truncation?
Figured something: Math.round(x) returns a long, not a double. Hence: it is impossible for Math.round() to return a number looking like 3.9999998. Therefore, int(Math.round()) will never need to truncate anything and will always work.
is there a possibility that casting a double created via Math.round() will still result in a truncated down number
No, round() will always round your double to the correct value, and then, it will be cast to an long which will truncate any decimal places. But after rounding, there will not be any fractional parts remaining.
Here are the docs from Math.round(double):
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
For the datatype Double to int, you can use the following:
Double double = 5.00;
int integer = double.intValue();
Double perValue = 96.57;
int roundVal= (int) Math.round(perValue);
Solved my purpose.

Categories

Resources