Home > database >  Count how many numbers are in a number ( division of float numbers)
Count how many numbers are in a number ( division of float numbers)

Time:11-21

I recently came to this language, and I do not fully understand how some actions work.

package com.example.carapp;

public class Calculate {
    static int[] benzmoney= {12,8,10};
    static float[] factor = {1F, 0.5F,0.8F};
    public static float calculateresult(int position,float fresult) {
        if (fresult == 0) {
            return 0;
        } else if (fresult < 10000){
            return ("something");
        } else{
            float v = (fresult * factor[position]) / 10000 * 6300   (fresult * factor[position]) / 40000 * 11000   (fresult * factor[position]) / 80000 * 21000   (fresult * factor[position]) / 150000 * 7000;
            return v;
        }
    }
    public static float calculatebenz(int position,float fresult,float cost){
        float a=(fresult/100)*cost*benzmoney[position];
        return a;
    }
}

A number will be entered into the column on the screen, and I will have to count how many times it will contain the number 10000, 40000, 80000, 150000. These numbers indicate the mileage of the car, certain parts need to be changed at these kilometers. the quantity will be calculated and multiplied by the cost of the parts. I assumed that if I divide the original number by each of them completely, and multiply by the amount I need, I will get the desired result. But, as I found out, 10000/50000 = 0.2 instead of 0. How can I solve this problem, so that two fractional numbers would be divided by each other entirely, would not give something other than zero, if the second number is greater. In this line: float v = (fresult * factor[position]) / 10000 * 6300 (fresult * factor[position]) / 40000 * 11000 (fresult * factor[position]) / 80000 * 21000 (fresult * factor[position]) / 150000 * 7000;

everything I tried didn't work

CodePudding user response:

Without much conversion and easiest way is Math.floor(result) to round to lover end of result (which you need) or Math.round(result) to nearest whole number. From your question you need Math.floor because if you think about it, you need how much whole cycles have passed.

So, for example:

a = 10_000
b = 50_000
c = a / b = 0.2
Math.round(c) = 0
Math.floor(c) = 0


a = 10_000
b = 11_000
c = a / b = 0.91
Math.round(c) = 1
Math.floor(c) = 0

So, where you do return v, do return Math.floor(v) instead

CodePudding user response:

If you want the result to truncate to an integer (drop the remainder) then that's integer division and it requires both numbers to be ints (or longs). If either of them is a floating-point number, it uses floating-point arithmetic and the result is a floating-point type.

It's common to have one kind of number but want to treat it as another, e.g. you have 5 and you want to divide it by 2 but you want 2.5 for your result. In this case, you can cast the primitive to a different type:

int num1 = 5;
int num2 = 2;
float result = num1 / (float) num2;

Because one of them being floating-point makes the whole thing a floating-point calculation, all we have to do here is cast one to float. You can do the same with floats to do integer arithmetic, but in that case you need to cast both (so neither of them are floating-point):

float num1 = 5f;
float num2 = 2f;
int result = (int) num1 / (int) num2;

Really what's happening is the narrower primitive is being widened to match the other operand, and that wider type is what's returned. So int / int doesn't require any widening and returns an int, int * float becomes float * float, double float becomes double double etc. More info here

laban_luca's answer is important to know - the Math package and its various functions (including different rounding methods) are really useful, but this is how you wrangle numbers for basic arithmetic. And it's important to get it right! Dividing two ints and wondering why you're not getting a fraction is a pretty common mistake, when you forget to cast one

  • Related