Predict_it
Predict_it

Reputation: 233

How can I properly output the correct double value?

So I'm trying to enhance these types to get output d = 5.5, I tried different types and what I get is 5.0 , what should I do to get correct output?

    int a = 5;
    int b = 4;
    int c = 3;
    int e = 2;
    double d = (double)(a) + b/c/e;
    System.out.println(d);

Upvotes: 0

Views: 59

Answers (3)

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Is that what you looking for?:

double d = (double)(a) + ((double)(b/c))/(e);

Upvotes: 1

Linora
Linora

Reputation: 10988

double a = 5.0;
double b = 4.0;
double c = 4.0;
double  e = 2.0;
double d = a + b/c/e;
System.out.println(d);

This will give 5.5 :)

Upvotes: 1

La-comadreja
La-comadreja

Reputation: 5755

double d = a + (double)(e / b);

Upvotes: 1

Related Questions