Convert fractions to decimal

I am doing some exercises in my Java book. I am very new to programming. Therefore, note (in code) that I am still in the first chapter. Now I’ve done everything, I just want confirmation if this is legal, so I can move on freely.

If not, I would sincerely acknowledge not my code for me; I want some advice.

Here is the question written in the book , "Write down the application that queries / reads the numerator and denominator of the fraction as integers, then prints the decimal equivalent of the fractional part."

I will illustrate this sentence with my code:

I did a revision here. This is normal?..

import java.util.*; public class ExerciseEleven { public static void main (String[] args) { Scanner sc = new Scanner (System.in); double fraction; int fractionValue; int decimal; double value; System.out.println("Enter Numerator: "); int numerator = sc.nextInt(); System.out.println("Enter Denominator: "); int denominator = sc.nextInt(); fraction = (double) numerator / denominator; fractionValue = (int) (fraction * 10); decimal = fractionValue % 10; value = decimal * 0.1; System.out.println(value); } } 

It compiles and works fine. Thanks.

+5
source share
4 answers

He does not fulfill what the task says. You read doubles instead of integers, and the decimal equivalent is not what you print. The decimal equivalent for 1/2 is 0.5. And you type 5.

In addition, you can pay attention to your code style: variable names are usually written in lowerCamelCase, for example: simpleVariable .

Update

Now it prints what you need. However, you are not doing this very well, and your indentation can still be improved.

+10
source

It's subtle (I didn't read the assignment very well, right?) Kudos to Vladimir .) ... but some comments:

  • Usually you want to use indentation methods inside the class.
  • The standard practice is to use initial caps ( Numerator ) only for types (for example, classes, interfaces, enumerations). Variable, field, and method names must begin with a lowercase letter. Now you can ignore standard practice, but if you have a lot of problems reading your code. :-)
  • For rounding, you probably want to look at Math.round , rather than slice the application. But the assignment did not say anything about rounding.
  • You might want to handle the case where the denominator is zero.

Therefore, remembering this:

 import java.util.*; public class ExcerciseEleven { public static void main (String[] args) { Scanner sc = new Scanner (System.in); System.out.println("Enter Numerator: "); int numerator = sc.nextInt(); System.out.println("Enter Denominator: "); int denominator = sc.nextInt(); if (denominator == 0) { System.out.println("Can't divide by zero"); } else { double fraction = (double)numerator / denominator; System.out.println(fraction); } } } 
+3
source

Hey, I think a little about it, and I noticed something interesting, looking at this source, and here is the Algorithm that I plan to implement

  • First, I convert the number from the metric using the Javax.Measure family of functions, and I get a number like 0.3750
  • Then I divide the number by ONE_SIXTEENTH, which = 0.0625
    ONE_SIXTEENTH = 0.0625
    Answer 0.3750 / ONE_SIXTEENTH = 6;
  • So now I know that there are 6 sixteenths of an inch
  • Next, I check if 6 is divisible by 4, 6/4 = 1.5, i.e. not an integer, so still the fraction is still considered 6/16 in.
  • Next, I check if 6 is divisible by 2, 6/2 = 3. This is an integer, so we will use it to restore the fraction
  • So now that we have divided 6 by 2 and got 3, 16 needs to be divided by 2, and we get 8, so that the 6/16th inch becomes 3/8 inch inch

PS Has anyone noticed that this looks like a fizz bang program?

____________________________________________

Here is a chart that helped me figure this out

So I developed how I am going to do it.
My works

0
source

The division operation consists of three important parts:

  1. Sign of the result.
  2. Component
  3. Decimal part

In addition, there are several angular cases where you need to consider the fact that Integer.MIN_VALUE larger than Integer.MAX_VALUE when comparing in absolute form.

For example: -2147483648/-1 cannot produce 2147483648 when dividing as integer types. The reason is simple. The type of the resulting type will be integer, and the maximum positive value that the variable of the integer type may contain is +2147483647

To mitigate this scenario, we must first convert the numerator and denominator into their long positive form. This gives us an integral part of the answer.

XOR of two numbers will have a sign bit as 1 only if they have opposite signs. This solves the first part ( sign of the result) of the problem.

For the decimal part, we can use the general division rule, that is, multiply the remainder by 10 and try again to divide and repeat. Write down the remainder that we have already encountered in order to prevent the loop from transitioning to unlimited iterations.

 public String fractionToDecimal(int A, int B) { StringBuilder sb = new StringBuilder((A^B) < 0 ? "-" : ""); long a = Math.abs((long)A); long b = Math.abs((long)B); sb.append(Long.toString(a/b)); long rem = a % b; sb.append((rem != 0) ? "." : ""); Map<Long, Integer> remainderMap = new HashMap<>(); int pos = 0; while (rem != 0){ sb.append(Long.toString((rem*10)/b)); remainderMap.put(rem, pos++); rem = (rem*10) % b; if (remainderMap.containsKey(rem)){ String currNum[] = sb.toString().split("\\."); return currNum[0] + "." + currNum[1].substring(0, remainderMap.get(rem)) + "(" + currNum[1].substring(remainderMap.get(rem)) + ")"; } } if (sb.toString().equals("-0")) return "0"; return sb.toString(); } 

Output Example:

  • 2/3 gives 0.(6)

  • -2147483648/-1 gives 2147483648

0
source

Source: https://habr.com/ru/post/1333460/


All Articles