Format a fraction in java [closed] - java

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 4 years ago.
Improve this question
Is there any way to print a fraction in any form? I'm working on a math program and a fraction is much easier to read than 1/2.
If there is any API that can do written math, I would also be glad.
Example:
Instead of 1/2

There isn't a one-liner answer for your question, so try approaching it from a different angle. You could write a method that takes the numerator and denominator as arguments and then return an output string in any format you want, such as String.format("%d\n---\n%d",numerator,denominator);

if your printing it without quotes your pretty much just turning it into a decimal because your dividing the 2. An example to avoid this:
System.out.println("1/2");

Related

Multiplying a string containing numbers with another string containing same? [closed]

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 2 years ago.
Improve this question
Suppose I have a string
String string = "1.18";
I want to multiply the above string with another string that is
String f1= "2.54";
What would I have to do to multiply these two values? I looked this up online and came across a function called Integer.ParseInt() that converts the string to it's numerical values, so i tried
System.out.println(Integer.parseInt(string) * Integer.parseInt(f1));
It doesn't work. I want to know what am I doing wrong. What's the proper way of doing it? I would find it very helpful if someone could help me understand, thanks!
Try Double.parseDouble(string)
Integer is not for decimal number. Use Double.parseDouble() or use BigDecimal type if you need exact result.

Get a Java string of form (num/num), to become two double values [closed]

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 6 years ago.
Improve this question
For example, if I have a string (24/25), how would I go about being left with two double values of 24 and 25. My objective to to eventually, given a set of number of that form, divide each of them, and add to get the average. I'm fairly new to Java and I honestly am so confused as where to even begin. Thank you!
As Andreas said. First you need to take the substring between brackets. Use String.substr
Now split 24/25 to 24 and 25. You can use String.split
Then you can parse them with Double.parse

How to calculate number of 1 bit's in a BigInteger ? [closed]

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
I am solving a problem for which i require to know the number of 1 bit's in a BigInteger.
Thanks in advance.
You can use .bitCount() on the BigInteger. Unless you need to solve it manually, in which case you can use normal Java bitwise operations.
Do x-or with 0. By this you will get the bits set as 1 wherever there was 1 in your original input. Then you can count the number of bits set in the output.

how to assign huge value variable [closed]

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
it is possible to assign that big number in java? i need to make a calculation of 39 digits value. could any help? Thanks
Problem:
Consider the following composite number:
340282367237851113557325445936183246849
Write a Java method to find two numbers whose product is the above number.
I guess you need to check out the BigInteger of the java API. That might be able to store your results of those much big numbers. Read the documentation,
http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

how to solve calculations in java [closed]

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 9 years ago.
Improve this question
I'm working on a calculator, it should receive the input as a String and then perform the calculation, outputting the result.
For example, the input could be
((23+17) mod 7 × 4 AND 13
and the output would be 4, as expected.
How can I parse the input, to extract all the operands and perform the calculation ?
The other answers are just "how to set a variable to this result" but if you're actually looking to parse input, you should refer to this:
Equation (expression) parser with precedence?
There are a number of ways to go about solving this kind of problem, and a number of algorithms for doing so. Some of them are stack based, some perform a descent of the "tree", and I can even think of a (somewhat) convoluted way to OOP-ize it. I would start with the link above.
You can use regular expressions to parse the string.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
First you have to look for the most important arguments like [() - parentheses], then less [*/] and [+-]. You have to divide the whole string into parts.
Examples:
Simple calculator (bottom of the page)
Another calculator
Both in Java.
see this page for your reference.
http://www.tutorialspoint.com/java/java_basic_operators.htm
all operators in java are explained very clearly.

Categories