# Thread: arithmetic with doubles problem

1. Member
Join Date
Oct 2010
Posts
45
Rep Power
0

## arithmetic with doubles problem

hello so i just wrote this code

Java Code:
```public static void main(String[] args) {

int[] meals = {0,1,1,1,1};
double cost = 0.0;

int i =0;
while (i==0)
{
if (meals[i] == 1 )
cost += 1.75;
else if (meals[i] == 2)
cost  += 3.83;
else if (meals[i] == 3)
cost += 5.58;
else if (meals[i] == 4)
cost += 7.66;
else if (meals[i] == 0)
cost += 0;
else
cost += 9.41;
i++;
}
while (i==1)
{
if (meals[i] == 1 )
cost += 1.75;
else if (meals[i] == 2)
cost  += 2*1.75;
else if (meals[i] == 3)
cost += 1.75 * 3;
else if (meals[i] == 4)
cost += 1.75 * 4;
else if (meals[i] == 0)
cost += 0;
else
cost += 1.75 * 5;
i++;
}
while (i==2)
{
if (meals[i] == 1 )
cost += 2.08;
else if (meals[i] == 2)
cost  += 2*2.08;
else if (meals[i] == 3)
cost += 2.08 * 3;
else if (meals[i] == 4)
cost += 2.08 * 4;
else if (meals[i] == 0)
cost += 0;
else
cost += 2.08 * 5;
i++;
}
while (i==3)
{
if (meals[i] == 1 )
cost += 2.14;
else if (meals[i] == 2)
cost  += 2*2.14;
else if (meals[i] == 3)
cost += 2.14 * 3;
else if (meals[i] == 4)
cost += 2.14 * 4;
else if (meals[i] == 0)
cost += 0;
else
cost +=2.14 * 5;
i++;
}
while (i==4)
{
if (meals[i] == 1 )
cost += 0.98;
else if (meals[i] == 2)
cost  += 2*0.98;
else if (meals[i] == 3)
cost += 0.98 * 3;
else if (meals[i] == 4)
cost += 0.98 * 4;
else if (meals[i] == 0)
cost += 0;
else
cost +=0.98 * 5;
i++;

}
System.out.print(cost);
}

}```
and the result i get it is 6.950000000000001
instead of 6.95, is there a way that i can get it to 6.95 without decimal formating?
Last edited by Fubarable; 10-04-2010 at 04:20 AM. Reason: bottom code tag corrected

2. Mult by 100, cast to int, divide by 100

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•