-2

So for my project I have to write a method to convert double values to a string. I understand how to do this multiple ways multiple options of formatting. What I'm confused about is how would I turn half sizes(shoes) into a fraction version while still converting it to a string. So 10.5 returns 10 - 1/2

Any tips or helpful pointers are appreciated. Sorry if this is a dumb question I am still learning. :)

3 Answers3

1

What you want can be done with the modulus operator (%) which in Java can be used with floating point values. If you do shoeSize % 1 you will get 0.5 with half sizes and 0.0 with others, so you only have to check that value to add the "1/2" to the string representation or not. Here is a simple example

public class ShoeSize {
    double size;
    public ShoeSize(double size) { this.size = size; }

    public String toString() {
        return "" + (int)size + (size % 1 == 0.5? " 1/2" : "");
    }

    public static void main(String args[]) {
        ShoeSize ss1 = new ShoeSize(10.5);
        ShoeSize ss2 = new ShoeSize(11);
        ShoeSize ss3 = new ShoeSize(11.5);

        System.out.println(ss1);
        System.out.println(ss2);
        System.out.println(ss3);
    }
}

The result of the previous code is:

10 1/2
11
11 1/2

However, you really shouldn't go with that approach. That is because depending on the way you manage the shoe size values it can lead to unpredictable results, just because arithmetic in floating point values is not precise as it is with integer values. Some simple operations like the following can introduce enough error so the result is not what you expect:

...

public static void main(String args[]) {
    ShoeSize ss = new ShoeSize(10.0);
    ss.size += 0.1 + 0.2 + 0.3; // Sum it half

    System.out.println(ss);
}

This code now prints 10 instead of 10 1/2.

What should you do instead? Well, there are several ways. You could for example store the shoe size inside ints representing, with each unit representing a half. This internal representation will be much error-prone if you have operations like addSize or subtracts. The only problem will be reading the size of the user; the best way is probably having a list of predefined sizes for the user to choose. Here is an example:

public class ShoeSize {
    int halves;

    public ShoeSize(double size) { this.halves = (int)(size * 2); }

    public String toString() {
        return "" + (halves / 2) + (halves % 2 == 1? " 1/2": "");
    }

    public static void main(String args[]) {
        ShoeSize ss = new ShoeSize(10.5);
        System.out.println(ss);
    }
}

Still better, since the shoe sizes use to be very restricted between certain values, you could represent their values in a single enum. Every enum can be constructed from the human-readable string of the size (ex. "10 1/2") and there would never be problems with invalid shoe sizes. The only problem with this approach is the need to define a custom method to obtain the next and previous shoe sizes, but here is a question that can help you with that: [What's the best way to implement `next` and `previous` on an enum type?

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
0

Try this

public String getShoeSize(double size) {
    int s = (int)size;
    String shoeSize = Integer.toString(s);
    //check if it is a half value
    if(size > s) {
        shoeSize += " - 1/2";
    }
    return shoeSize;
}
testerting
  • 65
  • 5
0

At first convert value of double to string and split it:

double d=10.5;
String s = String.valueOf(d);
String[] newstr =s.split(".");

then if fraction is limited use switch case for .5 , .25 , .75 but it is not limited ,simplifying fractions is easy if you can folow the steps: pay attention to this point that maybe your number is not point sign for example 10 that in switch case must be considered

1.find gcd of both num and den, so you have gcd=GCDFind(gcd, num);

2.now, if gcd==1, then the fraction cannot be simplified (it is already in simplified form).

3.if gcd > 1, then newNum = num/gcd; and newDen = den/gcd;

example for limited :

double s = 10.25;
        String aString = Double.toString(s);
        String[] fraction = aString.split("\\.");

        int denominator = (int)Math.pow(10, fraction[1].length());
        int numerator = Integer.parseInt(fraction[0] + "" + fraction[1]);
        int t=numerator%denominator;

        switch(t){
            case 0: System.out.println(numerator/denominator);break; //example : 10
            case 5: System.out.println(numerator/denominator + " - " +"1/2");break; //example : 10.5
            case 25: System.out.println(numerator/denominator + " - " +"1/4");break; //example : 10.25
            case 75: System.out.println(numerator/denominator + " - " +"3/4");break; //example : 10.75
            default:System.out.println("Not in 1/2, 1/4 or 3/4");
        }