Help with DecimalFormat
Hello there,
Can anyone point me to the right direction please?
I am trying to divide the total of a bill for a meal at a restaurant for the number of people having the meal.
For instance I have a total bill of ?4.50 (Pound Sterling) and 3 people that want to split the bill.
When I run my program I get:
3 guests @ ?4.83
Unfortunately I lose 1 pence.
how can I allocate the missing pence to one of the people paying the bill?
This is what I did:
DecimalFormat df1;
df1 = new DecimalFormat("####.00");
//this method split the bill in equal parts
df1.format(cRest.splitBill(aTable)));
//this method is invoked by the method above
public double splitBill(Table aTable)
{
bTable = aTable;
int noOfGuests = Integer.parseInt(bTable.getGuestNo());
aBill = bTable.getBill();
double guestsPayment =aBill.split(noOfGuests);//see method below
System.out.println("each guest payment is: "+ guestsPayment);
return guestsPayment;
}
//this method splits the bill
public doublesplit (int guestNo)
{
return total = total/guestNo;// do I need to modify this?
}
Any suggestions?
Thanks
[1268 byte] By [
Max010a] at [2007-10-3 6:41:25]

> //this method splits the bill
> public double split (int guestNo)
>{
> return total = total/guestNo;// do I need to modify
> this?
> }
>
> Any suggestions?
> Thanks
Yes, u need to modify this function to satisfy ur reqs. Do these steps
1. Devide the bill
2. Add all the individual amounts
3. If u r not getting the exact total amount then add that extra amount to a person by choosing randomly.
Cheers
Thank you very muchI ll give it a go.Rgrds
Hi again,
I ve tried what you suggested but it does not work.
private double amountDiff;
amountDiff = 0.00;
public double split (int guestNo)
{
double splitAmount;
splitAmount = total/guestNo;
//below do I have to add each amount (splitAmount)
// instead of multiplying?
if(splitAmount * guestNo != total)
{
//this condition is never executed why?
setAmountDiff(total - (splitAmount * guestNo));
System.out.println("the amount difference is: "+getAmountDiff());
}
System.out.println("the amount to be splitted is: "+splitAmount);
return splitAmount;
}
the output I get is:
the amount to be splitted is: 15.166666666666666
The total of the bill was ?5.50. On the GUI instead i get:
3 Guests @?5.17
What am I doing wrong?Do I need to round up the double precision?
Thanks
Here's a bit of a messy hack:
public double[] split(double amount, int numPeople) {
if(numPeople < 1)
throw new RuntimeException("numPeople < 1");
double[] array = new double[numPeople];
double subTotal = 0.0;
for(int i = 0; i < numPeople-1; i++) {
double temp = ((double)((int)((amount/numPeople)*100)))/100;
subTotal += temp;
array[i] = temp;
}
array[numPeople-1] = (amount - subTotal);
return array;
}
Maybe it's of use to you.
Good luck.
Thanks for the code.I am trying to understand what it does.Cheers
You need to use code tags (see button above posting box), first of all (to format your code nicely).
Second, you probably need to convert splitAmount to the rounded number (15.16), and then try the following:
if(splitAmount * guestNo != total)
Or, better:
if(splitAmount * guestNo < total)
Or, even better, try turning the input (45.50) into an int (4550) of pence. Then use int division. Then your answer will be 1516, three times 1516 will be 4548, and you have two pence to allocate to two of the three guests.
> Thanks for the code.
>
> I am trying to understand what it does.
>
> Cheers
Note that in his code, one person has to pay 2 pence more than the other two. You should split those 2 pence so that the difference in what the people pay is only 1 pence (15.16 from one person, 15.17 from each of two people).
Though, if anyone complains about 2 pence, they're probably very picky. :-P
> 3. If u r not getting the exact total amount then add
> that extra amount to a person by choosing randomly.
This is insufficient in the general case. In the general case, you might end up adding several pence to one individual's share, rather than appropriately splitting the overage equally among several individuals.
Here's the general algorithm.
N = number of people remaining to split the bill
T = total bill not yet split among remaining people
1) Let N1 = N (original number of people)
2) For n ranging from 1 to N1
3) Let X = equal T / N (rounded off to the nearest pence/cent)
4) Assign X to person n's share, where n is the next person
5) Let T = T - X (the amount of the bill not yet split up)
6) Let N = N - 1 (the remaining number of people to split it among), and repeat the loop established at 2)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
class BillShare {
public static void main(String args[]) throws Exception {
DecimalFormat df = new DecimalFormat("####.00");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double totAmnt;
System.out.println("Enter the Total Amount : ");
totAmnt = Double.parseDouble(br.readLine());
int noOfPeople = 3;
totAmnt = totAmnt * 100;
double sharedAmnt[] = new double[noOfPeople];
int x = 0;
while(x < totAmnt){
for(int i=0; i<noOfPeople; i++) {
sharedAmnt[i] = sharedAmnt[i] + 1;
x++;
if(x >= totAmnt) break;
}
}
for(int i=0; i<noOfPeople; i++) sharedAmnt[i] = sharedAmnt[i]/100;
for(int i=0; i><noOfPeople; i++) System.out.println("Bill-" + (i+1) + " : " + df.format(sharedAmnt[i]));
totAmnt = 0;
for(int i=0; i><noOfPeople; i++) totAmnt = totAmnt + sharedAmnt[i];
System.out.println("Total Amount : " + df.format(totAmnt));
}
}
Cheers>
1. Convert the total amount to pense2. Start to give each pense to persons one by one in cyclic order till you have no amount in your handThats the logic behind the above code. Hope its clear now. Cheers