Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How to round up a decimal number to a whole number?
So for example when it is 1.25 rounds to 1 or -3.25 rounds to -3 etc
if you want decimal part do this .
double value = 1.25;
int i = (int)value;
if you want to round value , do this
Math.round(value);
Use Math.round(float number) method http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round(float) if you want to round it. If you want to cut decimal part just cast it to int.
A small trick I've learned during the years is just adding the value 0.5 to the value you want to round. After you did that, you can round it to an integer.
It's a generic workaround, which will work with alot of programming languages.
I dont know much about Java, but I think you will find a round-Method in the API. The package should be Math.
Edit:\
To round UP you could just add 1 instead of the value 0.5.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
To help Beta Rabbit crack the lock, write a function answer(n) which returns the smallest positive integer base b, at least 2, in which the integer n is a palindrome. The input n will satisfy "0 <= n <= 1000."
Test cases
Inputs:
(int) n = 0
Output:
(int) 2
Inputs:
(int) n = 42
Output:
(int) 4
it is not the problem I need help with as there are similar ones on this site. It is the actual question. this input n they talk about, what the hell does it stand for? It can't be the base as b represents the base. It is not the positive integer they want for output because it is independent of that number and in the test cases it doesn't seem to have any correlation with it. Does it just want me to find the smallest palindrome of any base that's less than 1000 in decimal? Thanks to anyone that takes the time to help me figure this out , it's part of Google's foobar questions. I don't mind doing the work to solve the flecking thing as long as I can understand what the thing is asking.
What is Google Foobar?
I may not give you a full answer, but think about it this way. The number n, convert it to the output base... see if it's a palindrome
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.
For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.
I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.
The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0 means the number is even.
For more information please check Wikipedia.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
When I click on button the result display as Nan. And if I provide value of T directly for Example T=15 than 15 will be print out.
This is Formula.
T = (P*R)/ ((S*E)-(0.6)*P)
Where P , R , S is variable from Text Box.
And E is variable from Drop Down Box.
From my perspective, T is going to be a decimal, so the first thing you should look into is how to cast from a Java String to a double, using the Double.parseDouble() method, which you can read about here.
If you don't know how to grab the value from the text box itself, then you should read about JTextField, and its methods here. Finally, if you don't know how to get a value from a JComboBox, then you can read this tutorial.
Once you've worked out how to get the value from the text box or drop down list and parse it into the correct data type, you can get onto performing the calculation.
double t = (p * r)/((s * e) - (0.6) * p);
You'll also notice I've given you very little code. This is because you've given me nothing to work with. Happy reading!
Check your input for S & E, if it is 0 (zero) then your output always show NaN what stands for 'Not A Number'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having trouble generating this code which I'm sure I'm just in a coder's block or something because it seems as though it should be easy but can't get it for the life of me.
I have a program which needs random numbers generated within a certain range which is to represent money. The stipulation is that the money need be in between 2 and 200 and represent only even dollars only so $2, $4, $6...so on. I have done extensive searching online which yield a bounty of code to represent random numbers in a range in java but not the part about being only even.
Any ideas?
If you wanted to be clever, you could make sure the least significant bit of the number is not set:
int num = (new Random().nextInt(199) & ~1) + 2;
This will ensure that the number is always even.
Thanks Eyal Shneider and Omaha
Marcelo's comment from the OP is the correct answer, though.
Get a number between 1-100, and multiple by 2:
int num = (new Random().nextInt(100) + 1) * 2;
int rand = new Random.nextInt(200);
int result = 2;
while(result < rand) {
result += 2;
}
return result;
(I'd make it recursive but I've got to go meet my wife for dinner.)