tags:

views:

182

answers:

7

I'm trying to use the follow code to produce a decimal number, but the evaluation of l divided by h (low by high) always comes out to be 0.

How can I correct this? Thanks!

EditText marketValLow = (EditText) findViewById(R.id.marketValLow);
            EditText marketValHigh = (EditText) findViewById(R.id.marketValHigh);
            String valLow = marketValLow.getText().toString();
            String valHigh = marketValHigh.getText().toString();
            int l = Integer.parseInt(valLow);
            int h = Integer.parseInt(valHigh);

            if (valLow.trim().equals("") || valHigh.trim().equals("")) {

                Toast.makeText(CurrentMarketValue.this, "You need to enter a high AND low." + valLowIndex, Toast.LENGTH_SHORT).show();

            } else if ((l / h) < .9) { 

                Toast.makeText(CurrentMarketValue.this, "The range between your value cannot be more than 10%." + (l / h), Toast.LENGTH_SHORT).show();

            }
+4  A: 
double val = ((double)l)/h
Dave McClelland
+1  A: 

It would appea ryou're using an integer where a float or double would be better.

atk
+2  A: 

it is a basic part of many type languages. Integer division shows the results of whole divisions. In your case you could make it 1.0 / h or typecast one of the numbers to float or double.

Adam
+6  A: 

(int / int) will result in a int.

Instead do: double result = ((double)l) / ((double) h); and instead of checking on (l / h) do a check on result instead.

Jes
Thanks everyone! You were all right!
Allen Gingrich
It is fair more complicated than needed but it is clear what it is doing.
Peter Lawrey
+4  A: 

You're doing integer division. Force one of the operands of / to be a double.

((double)l / h)
nos
+2  A: 

Just by way of contrast to all the other answers:

if ((l * 10) < (h * 9))

Not saying its better (in fact if l or h has the possibility of being greater than about 200 million it's bad), just different.

JeremyP
Worse is still worse. THat will not produce the desired result, to start with.
TomTom
@TomTom what do you mean, it looks right to me and @LarsH
Peter Lawrey
Ditto, this appears correct to me. However, the original condition is (I suppose) much closer to the business logic, which would make it superior by virtue of being clearer.
Kena
It has to be said that if I were doing this in a production situation I would use the same solution as everybody else. The main reason is that (as TomTom demonstrates) my answer obfuscates what you are trying to do. @TomTom: divide both sides of the inequality by 10 and h and see what you get.
JeremyP
I would have written if (l * 10 < h * 9) in any case. The * always comes before < .
Peter Lawrey
+1  A: 

I think JeremyP is right. TomTom why do you say it's wrong? The following are all equivalent, by properties of inequalities (multiplying both sides by the same positive amount... assuming h >= 0):

(l / h) < .9
l < (.9 * h)
(l * 10) < (h * 9)

The latter also has the nice property of not throwing an exception when h = 0.

LarsH
You need to cast l to double anyway, so you won't get an exception dividing by 0.
Peter Lawrey
@Peter Lawrey, can you elaborate on that? I don't understand. Casting `l` to double will avoid an exception when dividing `l` by 0?
LarsH
int l = 5; System.out.println((double) l / 0); doesn't produce an exception.
Peter Lawrey
Peter, good point, thanks for the reminder. It produces double.NaN. And what does (NaN < .9) evaluate to? I'm assuming False, which would work out ok in the above code.
LarsH