Java arithmetics for calculating a percentage

后端 未结 8 683
一个人的身影
一个人的身影 2021-01-21 14:00

I have a little problem in my java application.

I have to calculate the score they have when they finish, I use this method:

public Float ScoreProcent(in         


        
相关标签:
8条回答
  • 2021-01-21 14:13
    Float num = (float) (100 / questions * correct); 
    

    It is executed in following way:

    Float num = (float) ((100 / questions) * correct); 
    

    since type of questions and correct is int, values after decimal are not conisdered.

    Now, 100 /questions = 100 / 38 = 2 and, num = (Float)(2 * 28) = 76.

    That's what you are getting.

    0 讨论(0)
  • 2021-01-21 14:14

    Use the following:

    Float num = (float) ((100  * correct) / questions);
    
    0 讨论(0)
  • 2021-01-21 14:15

    Try this.

    float num = (correct * 100.0) / questions;
    
    0 讨论(0)
  • 2021-01-21 14:15

    I will try explain other way:

    float hoursWorked = (float) 32.5;
    final double HOURLY_RATE = 9.35;
    double wages = HOURLY_RATE * hoursWorked;
    final double TAX_RATE = 22; // percentage
    double taxDue = TAX_RATE * wages / 100;
    // to calculate using percentage need to divide by 100
    System.out.print(taxDue);
    
    0 讨论(0)
  • 2021-01-21 14:16
    Float num = (float) (100 / y * x);
    

    The problem is that you weren't using the float you just built before.

    Alternatively I suggest this :

    public float getScorePercent(int questions, int correct){
        return 100f * correct / questions;
    }
    

    It's generally advised to avoid Float and use float, except when you really need objects (that is rarely).

    0 讨论(0)
  • 2021-01-21 14:20

    Firstly, you shouldn't be using Float all over the place - you want float; there's no need to be boxing here.

    Secondly, you're not using x and y at all.

    Thirdly, I'd say the way you're expressing the equation is at the least confusing. It's possible that just changing to x and y would be fine, but I wouldn't - I'd change the whole way you're expressing yourself.

    Fourthly, you're violating Java naming conventions by writing a method name in PascalCase. You've also got a spelling mistake.

    Fixing all of these, you'd end with with something like:

    public static float getPercentageCorrect(int questions, int correct) {
        float proportionCorrect = ((float) correct) / ((float) questions);
        return proportionCorrect * 100;
    }
    

    I'd actually generalize this - it's not specific to "correct answers", so can be used for anything where it's some score out of a total:

    /**
     * Returns a proportion (n out of a total) as a percentage, in a float.
     */
    public static float getPercentage(int n, int total) {
        float proportion = ((float) n) / ((float) total);
        return proportion * 100;
    }
    

    As noted in comments, this could be written as:

    float proportion = (float) n / total;
    

    ... but then you need to know the precedence rules to validate it. I've included both casts explicitly to make it clear that I want to convert each operand to float before the division.

    0 讨论(0)
提交回复
热议问题