1

conversion error float to int

public void priceMaker(float perc) {
    float distance = (float) dist.getValue();
    if (distance >= (float) 75.0) {
        switch (distance) {
            case 5.0:
                float f_5 = (float) (80.0 * 5.0 * perc);
                label_f_5_result.setText("Rs.80 * 5 * " + perc + "( risk factor % ) = " + f_5);
                label_apx_delv_fee_result.setText("Rs." + f_5);
        }
    }
}

I'm retrieving a distance value (like 1.5, 5, 4.3 in kilometers) from a jSpinner in my GUI. I have already changed the jSpinner model to [Number]>>(float). However I keep getting an IDE error even after I cast the calculations to float type:

possible lossy conversion from float to int error

I'm still a Java beginner and I couldn't find a solution. However, if I change distance from float to int the error disappears but I want float values because a distance won't be a roundup.

ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
Lakshan
  • 29
  • 2
  • 5
  • 3
    Post code as text rather than pictures. – Basil Bourque Jan 24 '22 at 06:43
  • Okay., 'perc' is for percentage & my goal is to define a float value when calling the priceMaker(); (ex. if I use 0.15, then on case calculation it will be 80.0*5.0*0.15) – Lakshan Jan 24 '22 at 07:30
  • 2
    you cannot switch on a floating point number (only char, byte, short, int, String, or an enum (and their corresponding wrapper classes) are allowed) and floating point comparison can be *tricky* - floating point are not (always) exact (try `0.1 + 0.1 + 0.1 == 0.3` results in `false`) - `float` and `double` are, by specification, a *"roundup"* – user16320675 Jan 24 '22 at 07:40
  • Tnx man @user16320675 , I didn't know this thing. seems I have to go all the way with if-else if conditions right? or are there any other methods to perform this task? – Lakshan Jan 24 '22 at 08:05

1 Answers1

2

As said in the comments you cannot switch on variables of type float. Have a look at the switch documentation. In particular the initial part goes:

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types.

This is because the representation of float values is inaccurate by nature. If you are interested in knowing more about floating points and why this happens, have a look at this.

As for how you might solve your situation, yes, you should go for if-else statements. However keep in mind that you cannot use == for float comparison (see this for more info). Try something like this:

public void priceMaker(float perc) {
    float distance = (float) dist.getValue();
    if (Float.compare(distance, 5f) == 0) {
        // do something
    }
}

Go here for Float.compare() documentation.

ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
  • If this solved your issue, please consider accepting the answer by clicking on the check mark on the left side of it, and / or upvoting. Thanks – ClaudiaR Aug 23 '22 at 13:07